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
085869bf
Kaydet (Commit)
085869bf
authored
Kas 23, 2013
tarafından
Guido van Rossum
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
asyncio: Change bounded semaphore into a subclass, like threading.[Bounded]Semaphore.
üst
dcd340ee
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
20 additions
and
18 deletions
+20
-18
locks.py
Lib/asyncio/locks.py
+19
-17
test_locks.py
Lib/test/test_asyncio/test_locks.py
+1
-1
No files found.
Lib/asyncio/locks.py
Dosyayı görüntüle @
085869bf
...
...
@@ -336,22 +336,15 @@ class Semaphore:
Semaphores also support the context manager protocol.
The
first
optional argument gives the initial value for the internal
The optional argument gives the initial value for the internal
counter; it defaults to 1. If the value given is less than 0,
ValueError is raised.
The second optional argument determines if the semaphore can be released
more than initial internal counter value; it defaults to False. If the
value given is True and number of release() is more than number of
successful acquire() calls ValueError is raised.
"""
def
__init__
(
self
,
value
=
1
,
bound
=
False
,
*
,
loop
=
None
):
def
__init__
(
self
,
value
=
1
,
*
,
loop
=
None
):
if
value
<
0
:
raise
ValueError
(
"Semaphore initial value must be >= 0"
)
self
.
_value
=
value
self
.
_bound
=
bound
self
.
_bound_value
=
value
self
.
_waiters
=
collections
.
deque
()
self
.
_locked
=
(
value
==
0
)
if
loop
is
not
None
:
...
...
@@ -402,17 +395,9 @@ class Semaphore:
"""Release a semaphore, incrementing the internal counter by one.
When it was zero on entry and another coroutine is waiting for it to
become larger than zero again, wake up that coroutine.
If Semaphore is created with "bound" parameter equals true, then
release() method checks to make sure its current value doesn't exceed
its initial value. If it does, ValueError is raised.
"""
if
self
.
_bound
and
self
.
_value
>=
self
.
_bound_value
:
raise
ValueError
(
'Semaphore released too many times'
)
self
.
_value
+=
1
self
.
_locked
=
False
for
waiter
in
self
.
_waiters
:
if
not
waiter
.
done
():
waiter
.
set_result
(
True
)
...
...
@@ -429,3 +414,20 @@ class Semaphore:
def
__iter__
(
self
):
yield
from
self
.
acquire
()
return
self
class
BoundedSemaphore
(
Semaphore
):
"""A bounded semaphore implementation.
This raises ValueError in release() if it would increase the value
above the initial value.
"""
def
__init__
(
self
,
value
=
1
,
*
,
loop
=
None
):
self
.
_bound_value
=
value
super
()
.
__init__
(
value
,
loop
=
loop
)
def
release
(
self
):
if
self
.
_value
>=
self
.
_bound_value
:
raise
ValueError
(
'BoundedSemaphore released too many times'
)
super
()
.
release
()
Lib/test/test_asyncio/test_locks.py
Dosyayı görüntüle @
085869bf
...
...
@@ -805,7 +805,7 @@ class SemaphoreTests(unittest.TestCase):
self
.
assertFalse
(
sem
.
_waiters
)
def
test_release_not_acquired
(
self
):
sem
=
locks
.
Semaphore
(
bound
=
True
,
loop
=
self
.
loop
)
sem
=
locks
.
BoundedSemaphore
(
loop
=
self
.
loop
)
self
.
assertRaises
(
ValueError
,
sem
.
release
)
...
...
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