-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathconcurrent_blocking_requests.py
46 lines (33 loc) · 1.02 KB
/
concurrent_blocking_requests.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
42
43
44
45
import asyncio
import time
import random
import urllib.request
RAND_VARIATION = 5
URL = 'http://xkcd.com'
# Blocking HTTP GET
def download_url(url):
return urllib.request.urlopen(url).read()
@asyncio.coroutine
def worker_loop(hostid, interval):
count = 0
while True:
count += 1
print("Agent {} - loop count: {}".format(hostid, count))
# Call download_url in the thread pool.
loop = asyncio.get_event_loop()
fut = loop.run_in_executor(None, download_url, URL)
result = yield from fut
print("Agent {} Got data len: {}".format(hostid, len(result)))
yield from asyncio.sleep(interval + random.randint(0, RAND_VARIATION))
@asyncio.coroutine
def run_agents():
num_agents = 5
interval = 10
all_agents = [worker_loop(i, interval) for i in range(1, num_agents+1)]
yield from asyncio.gather(*all_agents)
def main():
loop = asyncio.get_event_loop()
loop.run_until_complete(run_agents())
loop.close()
if __name__ == '__main__':
main()