41
41
log = logging .getLogger (__name__ )
42
42
43
43
44
- def _cleanup (loop_weakref ):
45
- try :
46
- loop = loop_weakref ()
47
- except ReferenceError :
48
- return
49
- loop ._cleanup ()
44
+ def _cleanup (loop ):
45
+ if loop :
46
+ loop ._cleanup ()
50
47
51
48
52
49
class LibevLoop (object ):
@@ -84,8 +81,6 @@ def __init__(self):
84
81
self ._timers = TimerManager ()
85
82
self ._loop_timer = libev .Timer (self ._loop , self ._on_loop_timer )
86
83
87
- atexit .register (partial (_cleanup , weakref .ref (self )))
88
-
89
84
def maybe_start (self ):
90
85
should_start = False
91
86
with self ._lock :
@@ -228,36 +223,41 @@ def _loop_will_run(self, prepare):
228
223
self ._notifier .send ()
229
224
230
225
226
+ _global_loop = None
227
+ atexit .register (partial (_cleanup , _global_loop ))
228
+
229
+
231
230
class LibevConnection (Connection ):
232
231
"""
233
232
An implementation of :class:`.Connection` that uses libev for its event loop.
234
233
"""
235
- _libevloop = None
236
234
_write_watcher_is_active = False
237
235
_read_watcher = None
238
236
_write_watcher = None
239
237
_socket = None
240
238
241
239
@classmethod
242
240
def initialize_reactor (cls ):
243
- if not cls ._libevloop :
244
- cls ._libevloop = LibevLoop ()
241
+ global _global_loop
242
+ if not _global_loop :
243
+ _global_loop = LibevLoop ()
245
244
else :
246
- if cls . _libevloop ._pid != os .getpid ():
245
+ if _global_loop ._pid != os .getpid ():
247
246
log .debug ("Detected fork, clearing and reinitializing reactor state" )
248
247
cls .handle_fork ()
249
- cls . _libevloop = LibevLoop ()
248
+ _global_loop = LibevLoop ()
250
249
251
250
@classmethod
252
251
def handle_fork (cls ):
253
- if cls ._libevloop :
254
- cls ._libevloop ._cleanup ()
255
- cls ._libevloop = None
252
+ global _global_loop
253
+ if _global_loop :
254
+ _global_loop ._cleanup ()
255
+ _global_loop = None
256
256
257
257
@classmethod
258
258
def create_timer (cls , timeout , callback ):
259
259
timer = Timer (timeout , callback )
260
- cls . _libevloop .add_timer (timer )
260
+ _global_loop .add_timer (timer )
261
261
return timer
262
262
263
263
def __init__ (self , * args , ** kwargs ):
@@ -268,16 +268,16 @@ def __init__(self, *args, **kwargs):
268
268
self ._connect_socket ()
269
269
self ._socket .setblocking (0 )
270
270
271
- with self . _libevloop ._lock :
272
- self ._read_watcher = libev .IO (self ._socket .fileno (), libev .EV_READ , self . _libevloop ._loop , self .handle_read )
273
- self ._write_watcher = libev .IO (self ._socket .fileno (), libev .EV_WRITE , self . _libevloop ._loop , self .handle_write )
271
+ with _global_loop ._lock :
272
+ self ._read_watcher = libev .IO (self ._socket .fileno (), libev .EV_READ , _global_loop ._loop , self .handle_read )
273
+ self ._write_watcher = libev .IO (self ._socket .fileno (), libev .EV_WRITE , _global_loop ._loop , self .handle_write )
274
274
275
275
self ._send_options_message ()
276
276
277
- self . _libevloop .connection_created (self )
277
+ _global_loop .connection_created (self )
278
278
279
279
# start the global event loop if needed
280
- self . _libevloop .maybe_start ()
280
+ _global_loop .maybe_start ()
281
281
282
282
def close (self ):
283
283
with self .lock :
@@ -286,7 +286,8 @@ def close(self):
286
286
self .is_closed = True
287
287
288
288
log .debug ("Closing connection (%s) to %s" , id (self ), self .host )
289
- self ._libevloop .connection_destroyed (self )
289
+
290
+ _global_loop .connection_destroyed (self )
290
291
self ._socket .close ()
291
292
log .debug ("Closed socket to %s" , self .host )
292
293
@@ -367,4 +368,4 @@ def push(self, data):
367
368
368
369
with self ._deque_lock :
369
370
self .deque .extend (chunks )
370
- self . _libevloop .notify ()
371
+ _global_loop .notify ()
0 commit comments