Skip to content

Commit be8103a

Browse files
try concurrent.futures
1 parent d960727 commit be8103a

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

24-test5.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
from concurrent.futures import ThreadPoolExecutor
2+
import requests
3+
4+
5+
def task(url, timeout=10):
6+
return requests.get(url, timeout=timeout)
7+
8+
9+
def req(pool):
10+
URLS = ["https://www.baidu.com", "http://www.qq.com", "https://www.tencent.com"]
11+
results = pool.map(task, URLS)
12+
for result in results:
13+
print("{}, {}".format(result.url, len(result.content)))
14+
15+
16+
def insert_dic(dic, key, value):
17+
dic[key] = value
18+
19+
20+
def remove_dic(dic, key):
21+
del dic[key]
22+
23+
24+
def start_operation(dic, pool):
25+
pool.submit(insert_dic, dic, "hello", "world")
26+
27+
28+
def main():
29+
pool = ThreadPoolExecutor(max_workers=3)
30+
# req(pool)
31+
dic = {}
32+
start_operation(dic, pool)
33+
print(dic)
34+
35+
36+
if __name__ == '__main__':
37+
main()

0 commit comments

Comments
 (0)