-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCoroutine.py
More file actions
57 lines (39 loc) · 1.22 KB
/
Copy pathCoroutine.py
File metadata and controls
57 lines (39 loc) · 1.22 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
def finder(str_for_find):
while True:
s = (yield)
if str_for_find in s:
print('Found : "{}" in "{}"'.format(str_for_find, s))
f = finder('coroutine')
next(f)
f.send('PlanB')
f.send('This is coroutine')
########## Async / Await
import asyncio
import datetime
import random
# Async / Generator
@asyncio.coroutine
def display_date1(num, loop):
end_time = loop.time() + 50.0
while True:
print("Loop: {} Time: {}".format(num, datetime.datetime.now()))
if (loop.time() + 1.0) >= end_time:
break
yield from asyncio.sleep(random.randint(0, 5))
loop1 = asyncio.get_event_loop()
asyncio.ensure_future(display_date1(1, loop1))
asyncio.ensure_future(display_date1(2, loop1))
loop1.run_forever()
# Async / Await
async def display_date2(num, loop, ):
end_time = loop.time() + 50.0
while True:
print("Loop: {} Time: {}".format(num, datetime.datetime.now()))
if (loop.time() + 1.0) >= end_time:
break
await asyncio.sleep(random.randint(0, 5))
loop2 = asyncio.get_event_loop()
asyncio.ensure_future(display_date2(1, loop2))
asyncio.ensure_future(display_date2(2, loop2))
loop2.run_forever()
# from https://hamait.tistory.com/830