3
3
__all__ = ('Lock' , 'Event' , 'Condition' , 'Semaphore' , 'BoundedSemaphore' )
4
4
5
5
import collections
6
- import warnings
7
6
8
- from . import events
9
7
from . import exceptions
8
+ from . import mixins
10
9
11
10
12
11
class _ContextManagerMixin :
@@ -20,7 +19,7 @@ async def __aexit__(self, exc_type, exc, tb):
20
19
self .release ()
21
20
22
21
23
- class Lock (_ContextManagerMixin ):
22
+ class Lock (_ContextManagerMixin , mixins . _LoopBoundedMixin ):
24
23
"""Primitive lock objects.
25
24
26
25
A primitive lock is a synchronization primitive that is not owned
@@ -74,16 +73,9 @@ class Lock(_ContextManagerMixin):
74
73
75
74
"""
76
75
77
- def __init__ (self , * , loop = None ):
76
+ def __init__ (self ):
78
77
self ._waiters = None
79
78
self ._locked = False
80
- if loop is None :
81
- self ._loop = events .get_event_loop ()
82
- else :
83
- self ._loop = loop
84
- warnings .warn ("The loop argument is deprecated since Python 3.8, "
85
- "and scheduled for removal in Python 3.10." ,
86
- DeprecationWarning , stacklevel = 2 )
87
79
88
80
def __repr__ (self ):
89
81
res = super ().__repr__ ()
@@ -109,7 +101,7 @@ async def acquire(self):
109
101
110
102
if self ._waiters is None :
111
103
self ._waiters = collections .deque ()
112
- fut = self ._loop .create_future ()
104
+ fut = self ._get_loop () .create_future ()
113
105
self ._waiters .append (fut )
114
106
115
107
# Finally block should be called before the CancelledError
@@ -161,7 +153,7 @@ def _wake_up_first(self):
161
153
fut .set_result (True )
162
154
163
155
164
- class Event :
156
+ class Event ( mixins . _LoopBoundedMixin ) :
165
157
"""Asynchronous equivalent to threading.Event.
166
158
167
159
Class implementing event objects. An event manages a flag that can be set
@@ -170,16 +162,9 @@ class Event:
170
162
false.
171
163
"""
172
164
173
- def __init__ (self , * , loop = None ):
165
+ def __init__ (self ):
174
166
self ._waiters = collections .deque ()
175
167
self ._value = False
176
- if loop is None :
177
- self ._loop = events .get_event_loop ()
178
- else :
179
- self ._loop = loop
180
- warnings .warn ("The loop argument is deprecated since Python 3.8, "
181
- "and scheduled for removal in Python 3.10." ,
182
- DeprecationWarning , stacklevel = 2 )
183
168
184
169
def __repr__ (self ):
185
170
res = super ().__repr__ ()
@@ -220,7 +205,7 @@ async def wait(self):
220
205
if self ._value :
221
206
return True
222
207
223
- fut = self ._loop .create_future ()
208
+ fut = self ._get_loop () .create_future ()
224
209
self ._waiters .append (fut )
225
210
try :
226
211
await fut
@@ -229,7 +214,7 @@ async def wait(self):
229
214
self ._waiters .remove (fut )
230
215
231
216
232
- class Condition (_ContextManagerMixin ):
217
+ class Condition (_ContextManagerMixin , mixins . _LoopBoundedMixin ):
233
218
"""Asynchronous equivalent to threading.Condition.
234
219
235
220
This class implements condition variable objects. A condition variable
@@ -239,18 +224,10 @@ class Condition(_ContextManagerMixin):
239
224
A new Lock object is created and used as the underlying lock.
240
225
"""
241
226
242
- def __init__ (self , lock = None , * , loop = None ):
243
- if loop is None :
244
- self ._loop = events .get_event_loop ()
245
- else :
246
- self ._loop = loop
247
- warnings .warn ("The loop argument is deprecated since Python 3.8, "
248
- "and scheduled for removal in Python 3.10." ,
249
- DeprecationWarning , stacklevel = 2 )
250
-
227
+ def __init__ (self , lock = None ):
251
228
if lock is None :
252
- lock = Lock (loop = loop )
253
- elif lock ._loop is not self ._loop :
229
+ lock = Lock ()
230
+ elif lock ._loop is not self ._get_loop () :
254
231
raise ValueError ("loop argument must agree with lock" )
255
232
256
233
self ._lock = lock
@@ -284,7 +261,7 @@ async def wait(self):
284
261
285
262
self .release ()
286
263
try :
287
- fut = self ._loop .create_future ()
264
+ fut = self ._get_loop () .create_future ()
288
265
self ._waiters .append (fut )
289
266
try :
290
267
await fut
@@ -351,7 +328,7 @@ def notify_all(self):
351
328
self .notify (len (self ._waiters ))
352
329
353
330
354
- class Semaphore (_ContextManagerMixin ):
331
+ class Semaphore (_ContextManagerMixin , mixins . _LoopBoundedMixin ):
355
332
"""A Semaphore implementation.
356
333
357
334
A semaphore manages an internal counter which is decremented by each
@@ -366,18 +343,11 @@ class Semaphore(_ContextManagerMixin):
366
343
ValueError is raised.
367
344
"""
368
345
369
- def __init__ (self , value = 1 , * , loop = None ):
346
+ def __init__ (self , value = 1 ):
370
347
if value < 0 :
371
348
raise ValueError ("Semaphore initial value must be >= 0" )
372
349
self ._value = value
373
350
self ._waiters = collections .deque ()
374
- if loop is None :
375
- self ._loop = events .get_event_loop ()
376
- else :
377
- self ._loop = loop
378
- warnings .warn ("The loop argument is deprecated since Python 3.8, "
379
- "and scheduled for removal in Python 3.10." ,
380
- DeprecationWarning , stacklevel = 2 )
381
351
382
352
def __repr__ (self ):
383
353
res = super ().__repr__ ()
@@ -407,7 +377,7 @@ async def acquire(self):
407
377
True.
408
378
"""
409
379
while self ._value <= 0 :
410
- fut = self ._loop .create_future ()
380
+ fut = self ._get_loop () .create_future ()
411
381
self ._waiters .append (fut )
412
382
try :
413
383
await fut
@@ -436,14 +406,9 @@ class BoundedSemaphore(Semaphore):
436
406
above the initial value.
437
407
"""
438
408
439
- def __init__ (self , value = 1 , * , loop = None ):
440
- if loop :
441
- warnings .warn ("The loop argument is deprecated since Python 3.8, "
442
- "and scheduled for removal in Python 3.10." ,
443
- DeprecationWarning , stacklevel = 2 )
444
-
409
+ def __init__ (self , value = 1 ):
445
410
self ._bound_value = value
446
- super ().__init__ (value , loop = loop )
411
+ super ().__init__ (value )
447
412
448
413
def release (self ):
449
414
if self ._value >= self ._bound_value :
0 commit comments