Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
C
cpython
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
cpython
Commits
31d833d5
Kaydet (Commit)
31d833d5
authored
Agu 20, 2001
tarafından
Fred Drake
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Added documentation for BoundedSemaphore(), contributed by Skip Montanaro.
This closes SF patch #452836.
üst
0e40c3d0
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
40 additions
and
2 deletions
+40
-2
libthreading.tex
Doc/lib/libthreading.tex
+40
-2
No files found.
Doc/lib/libthreading.tex
Dosyayı görüntüle @
31d833d5
...
@@ -60,12 +60,22 @@ acquire it again without blocking; the thread must release it once
...
@@ -60,12 +60,22 @@ acquire it again without blocking; the thread must release it once
for each time it has acquired it.
for each time it has acquired it.
\end{funcdesc}
\end{funcdesc}
\begin{funcdesc}
{
Semaphore
}{}
\begin{funcdesc}
{
Semaphore
}{
\optional
{
value
}
}
A factory function that returns a new semaphore object. A
A factory function that returns a new semaphore object. A
semaphore manages a counter representing the number of
\method
{
release()
}
semaphore manages a counter representing the number of
\method
{
release()
}
calls minus the number of
\method
{
acquire()
}
calls, plus an initial value.
calls minus the number of
\method
{
acquire()
}
calls, plus an initial value.
The
\method
{
acquire()
}
method blocks if necessary until it can return
The
\method
{
acquire()
}
method blocks if necessary until it can return
without making the counter negative.
without making the counter negative. If not given,
\var
{
value
}
defaults to
1.
\end{funcdesc}
\begin{funcdesc}
{
BoundedSemaphore
}{
\optional
{
value
}}
A factory function that returns a new bounded semaphore object. A bounded
semaphore checks to make sure its current value doesn't exceed its initial
value. If it does,
\exception
{
ValueError
}
is raised. In most situations
semaphores are used to guard resources with limited capacity. If the
semaphore is released too many times it's a sign of a bug. If not given,
\var
{
value
}
defaults to 1.
\end{funcdesc}
\end{funcdesc}
\begin{classdesc*}
{
Thread
}{}
\begin{classdesc*}
{
Thread
}{}
...
@@ -367,6 +377,34 @@ than zero again, wake up that thread.
...
@@ -367,6 +377,34 @@ than zero again, wake up that thread.
\end{methoddesc}
\end{methoddesc}
\subsubsection
{
\class
{
Semaphore
}
Example
\label
{
semaphore-examples
}}
Semaphores are often used to guard resources with limited capacity, for
example, a database server. In any situation where the size of the resource
size is fixed, you should use a bounded semaphore. Before spawning any
worker threads, your main thread would initialize the semaphore:
\begin{verbatim}
maxconnections = 5
...
pool
_
sema = BoundedSemaphore(value=maxconnections)
\end{verbatim}
Once spawned, worker threads call the semaphore's acquire and release
methods when they need to connect to the server:
\begin{verbatim}
pool
_
sema.acquire()
conn = connectdb()
... use connection ...
conn.close()
pool
_
sema.release()
\end{verbatim}
The use of a bounded semaphore reduces the chance that a programming error
which causes the semaphore to be released more than it's acquired will go
undetected.
\subsection
{
Event Objects
\label
{
event-objects
}}
\subsection
{
Event Objects
\label
{
event-objects
}}
This is one of the simplest mechanisms for communication between
This is one of the simplest mechanisms for communication between
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment