@@ -11,95 +11,69 @@ def __init__(self):
11
11
self .poller = select .poll ()
12
12
self .objmap = {}
13
13
14
- def _unregister_fd (self , fd ):
15
- self .objmap .pop (fd , None )
16
- try :
17
- self .poller .unregister (fd )
18
- except OSError as e :
19
- if e .args [0 ] != errno .ENOENT :
20
- raise
21
-
22
- def remove_polled_cb (self , cb ):
23
- _id = id (cb )
24
- for fd , cbs in self .objmap .items ():
25
- cbs .pop (id (cb ), None )
26
- if not cbs :
27
- self ._unregister_fd (fd )
14
+ def remove_polled_cb (self , _id ):
15
+ for fd , cb in self .objmap .items ():
16
+ if id (cb ) == _id :
17
+ self .poller .unregister (fd )
18
+ break
28
19
29
20
def add_reader (self , fd , cb , * args ):
30
21
if __debug__ :
31
22
log .debug ("add_reader%s" , (fd , cb , args ))
32
- cbs = self .objmap .setdefault (fd , {})
33
- self .poller .register (fd , select .POLLIN )
34
23
if args :
35
- cbs [id (cb )] = (cb , args )
24
+ self .poller .register (fd , select .POLLIN )
25
+ self .objmap [fd ] = (cb , args )
36
26
else :
37
- cbs [id (cb )] = (cb , None )
27
+ self .poller .register (fd , select .POLLIN )
28
+ self .objmap [fd ] = cb
38
29
39
- def remove_reader (self , fd , cb ):
30
+ def remove_reader (self , fd ):
40
31
if __debug__ :
41
- log .debug ("remove_reader(%s)" , (fd , cb ))
42
- cbs = self .objmap .get (fd , {})
43
- cbs .pop (id (cb ), None )
44
- if not cbs :
45
- self ._unregister_fd (fd )
32
+ log .debug ("remove_reader(%s)" , fd )
33
+ self .poller .unregister (fd )
34
+ del self .objmap [fd ]
46
35
47
36
def add_writer (self , fd , cb , * args ):
48
37
if __debug__ :
49
38
log .debug ("add_writer%s" , (fd , cb , args ))
50
- cbs = self .objmap .setdefault (fd , {})
51
- self .poller .register (fd , select .POLLOUT )
52
39
if args :
53
- cbs [id (cb )] = (cb , args )
40
+ self .poller .register (fd , select .POLLOUT )
41
+ self .objmap [fd ] = (cb , args )
54
42
else :
55
- cbs [id (cb )] = (cb , None )
43
+ self .poller .register (fd , select .POLLOUT )
44
+ self .objmap [fd ] = cb
56
45
57
- def remove_writer (self , fd , cb ):
46
+ def remove_writer (self , fd ):
58
47
if __debug__ :
59
48
log .debug ("remove_writer(%s)" , fd )
60
- cbs = self .objmap .get (fd , {})
61
- cbs .pop (id (cb ), None )
62
- if not cbs :
63
- self ._unregister_fd (fd )
49
+ try :
50
+ self .poller .unregister (fd )
51
+ self .objmap .pop (fd , None )
52
+ except OSError as e :
53
+ # StreamWriter.awrite() first tries to write to an fd,
54
+ # and if that succeeds, yield IOWrite may never be called
55
+ # for that fd, and it will never be added to poller. So,
56
+ # ignore such error.
57
+ if e .args [0 ] != errno .ENOENT :
58
+ raise
64
59
65
60
def wait (self , delay ):
66
61
if __debug__ :
67
- log .debug ("epoll.wait(%s)" , delay )
68
- for fd , cbs in self .objmap .items ():
69
- for cb , args in cbs .values ():
70
- log .debug ("epoll.registered(%d) %s" , fd , (cb , args ))
71
-
62
+ log .debug ("epoll.wait(%d)" , delay )
72
63
# We need one-shot behavior (second arg of 1 to .poll())
73
64
if delay == - 1 :
74
65
res = self .poller .poll (- 1 , 1 )
75
66
else :
76
67
res = self .poller .poll (int (delay * 1000 ), 1 )
77
68
#log.debug("epoll result: %s", res)
78
69
for fd , ev in res :
79
- # Remove the registered callbacks dictionary from its parent
80
- # so when callbacks are invoked they can add their registrations
81
- # to a fresh dictionary.
82
- cbs = self .objmap .pop (fd , {})
83
- if not cbs :
84
- log .error ("Event %d on fd %r but no callback registered" , ev , fd )
85
- continue
70
+ cb = self .objmap [fd ]
86
71
if __debug__ :
87
- s = '\n ' .join (str (v ) for v in cbs .values ())
88
- log .debug ("Matching IO callbacks for %r:\n %s" , (fd , ev ), s )
89
- while cbs :
90
- _id , data = cbs .popitem ()
91
- cb , args = data
92
- if args is None :
93
- if __debug__ :
94
- log .debug ("Scheduling IO coro: %r" , (fd , ev , cb ))
95
- self .call_soon (cb )
96
- else :
97
- if __debug__ :
98
- log .debug ("Calling IO callback: %r" , (fd , ev , cb , args ))
99
- cb (* args )
100
- # If no callback registered an event for this fd unregister it
101
- if not self .objmap .get (fd , None ):
102
- self ._unregister_fd (fd )
72
+ log .debug ("Calling IO callback: %r" , cb )
73
+ if isinstance (cb , tuple ):
74
+ cb [0 ](* cb [1 ])
75
+ else :
76
+ self .call_soon (cb )
103
77
104
78
105
79
class StreamReader :
0 commit comments