我在python3中学习并发执行 .
我不知道为什么跟随http请求函数与时间一样,尽管有一个使用concurrent.futures.ThreadPoolExection,其他人没有 .
import urllib.request as ur
import concurrent.futures
def concurrent_exe(): # concurrent exection function that use ThreadPoolExecutor.
thread_pool_exe = concurrent.futures.ThreadPoolExecutor()
for v in range(10000):
future = thread_pool_exe.submit(access_example)
print(v, future.result())
def access_example():
return ur.urlopen("http://example.com").read().decode("utf-8")[10:15]
def seq_exe(): # sequential exectute function
for v in range(10000):
print(v, access_example())
我不知道为什么这些功能几乎同时发生 .
我的环境是:
-
Python 3.6
-
Virtual Box上的Ubuntu 18.10(主机操作系统:Windows10 1803)
1 回答
我知道我的代码应该改变什么 . future.result是阻塞方法 . 所以它应该等到完成第一个未来 .