-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.py
41 lines (33 loc) · 1.12 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import time
import math
from asynclib.core import Promise, LoopManager, Timer
from asynclib.asynchttp import get as asyncget
@LoopManager.asyncfun
def httpReq():
responseData = yield from Promise(
lambda resolve:
asyncget(
url='http://codingfor.life/doc/README.md',
callback=lambda response: resolve(response)
),
).catch(lambda e: print(e))
return responseData
@ LoopManager.asyncfun
def asyncmain():
# async call
print('async call start', '\n')
start = time.time()
print('async res: ', (yield from Promise.all([httpReq() for _ in range(10)])), '\n')
print('promise all cost: ' +
str(math.floor((time.time() - start) * 1000)) + 'ms', '\n')
# sleep a while using Timer
print('sleep 2000ms using coroutine timer', '\n')
yield from Timer.sleep(2000)
# sync call
print('sync call start', '\n')
start = time.time()
for _ in range(10):
print(f'sync res {_}: ', (yield from httpReq()), '\n')
print('sync cost: ' +
str(math.floor((time.time() - start) * 1000)) + 'ms', '\n')
asyncmain()