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
This repository was archived by the owner on Dec 2, 2021. It is now read-only.

Commit8bbaeb1

Browse files
committed
spinner example ported to async/await and curio
1 parentd7e37ad commit8bbaeb1

File tree

5 files changed

+104
-0
lines changed

5 files changed

+104
-0
lines changed

‎.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
*.sublime-project
22
*.sublime-workspace
3+
.env*
34
concurrency/flags/img/*.gif
45
concurrency/charfinder/charfinder_index.pickle
56
18-asyncio/charfinder/charfinder_index.pickle

‎18-asyncio/spinner_asyncio.py

100644100755
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#!/usr/bin/env python3
2+
13
# spinner_asyncio.py
24

35
# credits: Example by Luciano Ramalho inspired by

‎18-asyncio/spinner_await.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/usr/bin/env python3
2+
3+
# spinner_await.py
4+
5+
# credits: Example by Luciano Ramalho inspired by
6+
# Michele Simionato's multiprocessing example in the python-list:
7+
# https://mail.python.org/pipermail/python-list/2009-February/538048.html
8+
9+
importasyncio
10+
importitertools
11+
importsys
12+
13+
14+
asyncdefspin(msg):# <1>
15+
write,flush=sys.stdout.write,sys.stdout.flush
16+
forcharinitertools.cycle('|/-\\'):
17+
status=char+' '+msg
18+
write(status)
19+
flush()
20+
write('\x08'*len(status))
21+
try:
22+
awaitasyncio.sleep(.1)# <2>
23+
exceptasyncio.CancelledError:# <3>
24+
break
25+
write(' '*len(status)+'\x08'*len(status))
26+
27+
28+
asyncdefslow_function():# <4>
29+
# pretend waiting a long time for I/O
30+
awaitasyncio.sleep(3)# <5>
31+
return42
32+
33+
34+
asyncdefsupervisor():# <6>
35+
spinner=asyncio.ensure_future(spin('thinking!'))# <7>
36+
print('spinner object:',spinner)# <8>
37+
result=awaitslow_function()# <9>
38+
spinner.cancel()# <10>
39+
returnresult
40+
41+
42+
defmain():
43+
loop=asyncio.get_event_loop()# <11>
44+
result=loop.run_until_complete(supervisor())# <12>
45+
loop.close()
46+
print('Answer:',result)
47+
48+
49+
if__name__=='__main__':
50+
main()

‎18-asyncio/spinner_curio.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/usr/bin/env python3
2+
3+
# spinner_curio.py
4+
5+
# credits: Example by Luciano Ramalho inspired by
6+
# Michele Simionato's multiprocessing example in the python-list:
7+
# https://mail.python.org/pipermail/python-list/2009-February/538048.html
8+
9+
importcurio
10+
11+
importitertools
12+
importsys
13+
14+
15+
asyncdefspin(msg):# <1>
16+
write,flush=sys.stdout.write,sys.stdout.flush
17+
forcharinitertools.cycle('|/-\\'):
18+
status=char+' '+msg
19+
write(status)
20+
flush()
21+
write('\x08'*len(status))
22+
try:
23+
awaitcurio.sleep(.1)# <2>
24+
exceptcurio.CancelledError:# <3>
25+
break
26+
write(' '*len(status)+'\x08'*len(status))
27+
28+
29+
asyncdefslow_function():# <4>
30+
# pretend waiting a long time for I/O
31+
awaitcurio.sleep(3)# <5>
32+
return42
33+
34+
35+
asyncdefsupervisor():# <6>
36+
spinner=awaitcurio.spawn(spin('thinking!'))# <7>
37+
print('spinner object:\n ',repr(spinner))# <8>
38+
result=awaitslow_function()# <9>
39+
awaitspinner.cancel()# <10>
40+
returnresult
41+
42+
43+
defmain():
44+
result=curio.run(supervisor)# <12>
45+
print('Answer:',result)
46+
47+
48+
if__name__=='__main__':
49+
main()

‎18-asyncio/spinner_thread.py

100644100755
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#!/usr/bin/env python3
2+
13
# spinner_thread.py
24

35
# credits: Adapted from Michele Simionato's

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp