Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit5166ecb

Browse files
author
Paul Sokolovsky
committed
asyncio_slow: Start new upstream API-compatible asyncio implementation.
The trait of this implementation is that it doesn't use priority queue andtime scheduling, and instead does its all operations using polling, startingwith such basic one as sleep. On the other hand, this tries to implementall (well, much) of upstream asyncio API and warts.asyncio_slow: Rename from asyncio_micro.It may turn out that this won't be "micro" at all. The main trait of thisimplementation is that it stay 100% API compatible with upstream (inthose APIs which are implemented of course). It will also keep inefficientimplementation of event loop scheduling, to discourage its use. Here we go.
1 parent4c4a743 commit5166ecb

File tree

1 file changed

+144
-0
lines changed

1 file changed

+144
-0
lines changed

‎asyncio_slow/asyncio_slow.py‎

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
importtime
2+
importlogging
3+
4+
5+
log=logging.getLogger("asyncio")
6+
7+
8+
# Workaround for not being able to subclass builtin types
9+
DoneException=AssertionError
10+
11+
classInvalidStateError:
12+
pass
13+
14+
# Object not matching any other object
15+
_sentinel= []
16+
17+
18+
classEventLoop:
19+
20+
def__init__(self):
21+
self.q= []
22+
23+
defcall_soon(self,c,*args):
24+
self.q.append(c)
25+
26+
defrun_forever(self):
27+
whileself.q:
28+
c=self.q.pop(0)
29+
c()
30+
# I mean, forever
31+
whileTrue:
32+
time.sleep(1)
33+
34+
defrun_until_complete(self,coro):
35+
def_cb(val):
36+
raiseDoneException
37+
38+
t=async(coro)
39+
t.add_done_callback(_cb)
40+
self.call_soon(t)
41+
try:
42+
self.run_forever()
43+
exceptDoneException:
44+
pass
45+
46+
defclose(self):
47+
pass
48+
49+
50+
_def_event_loop=EventLoop()
51+
52+
53+
classFuture:
54+
55+
def__init__(self,loop=_def_event_loop):
56+
self.loop=loop
57+
self.res=_sentinel
58+
self.cbs= []
59+
60+
defresult(self):
61+
ifself.resis_sentinel:
62+
raiseInvalidStateError
63+
returnself.res
64+
65+
defadd_done_callback(self,fn):
66+
ifself.resis_sentinel:
67+
self.cbs.append(fn)
68+
else:
69+
self.loop.call_soon(fn,self)
70+
71+
defset_result(self,val):
72+
self.res=val
73+
forfinself.cbs:
74+
f(self)
75+
76+
77+
classTask(Future):
78+
79+
def__init__(self,coro,loop=_def_event_loop):
80+
super().__init__()
81+
self.loop=loop
82+
self.c=coro
83+
# upstream asyncio forces task to be scheduled on instantiation
84+
self.loop.call_soon(self)
85+
86+
def__call__(self):
87+
try:
88+
next(self.c)
89+
self.loop.call_soon(self)
90+
exceptStopIterationase:
91+
log.debug("Coro finished: %s",self.c)
92+
self.set_result(None)
93+
94+
95+
defget_event_loop():
96+
return_def_event_loop
97+
98+
99+
# Decorator
100+
defcoroutine(f):
101+
returnf
102+
103+
104+
defasync(coro):
105+
ifisinstance(coro,Future):
106+
returncoro
107+
returnTask(coro)
108+
109+
110+
classWait(Future):
111+
112+
def__init__(self,n):
113+
Future.__init__(self)
114+
self.n=n
115+
116+
def_done(self):
117+
self.n-=1
118+
log.debug("Wait: remaining tasks: %d",self.n)
119+
ifnotself.n:
120+
self.set_result(None)
121+
122+
def__call__(self):
123+
pass
124+
125+
126+
defwait(coro_list,loop=_def_event_loop):
127+
128+
w=Wait(len(coro_list))
129+
130+
forcincoro_list:
131+
t=async(c)
132+
t.add_done_callback(lambdaval:w._done())
133+
loop.call_soon(t)
134+
135+
returnw
136+
137+
138+
defsleep(secs):
139+
t=time.time()
140+
log.debug("Started sleep at: %s, targetting: %s",t,t+secs)
141+
whiletime.time()<t+secs:
142+
time.sleep(0.01)
143+
yield
144+
log.debug("Finished sleeping %ss",secs)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp