Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么并发http请求与顺序执行的时间相同?_Python_Python 3.x_Concurrency - Fatal编程技术网

Python 为什么并发http请求与顺序执行的时间相同?

Python 为什么并发http请求与顺序执行的时间相同?,python,python-3.x,concurrency,Python,Python 3.x,Concurrency,我正在学习python3中的并发执行。 我不知道为什么下面的http请求函数与时间相同,尽管其中一个使用concurrent.futures.ThreadPoolExecution,而另一个不使用 import urllib.request as ur import concurrent.futures def concurrent_exe(): # concurrent exection function that use ThreadPoolExecutor. thread_po

我正在学习python3中的并发执行。
我不知道为什么下面的http请求函数与时间相同,尽管其中一个使用concurrent.futures.ThreadPoolExecution,而另一个不使用

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

  • 虚拟机上的Ubuntu 18.10(主机操作系统:Windows10 1803)


    • 我知道我应该在代码中更改什么。 结果是阻塞方法。所以它应该等到第一次完成以后

      def concurrent_exe():
          futures_list = []
          thread_pool_exe = concurrent.futures.ThreadPoolExecutor()
          for v in range(10000):
              future = thread_pool_exe.submit(access_example, v)
              futures_list.append(future)
          for v in concurrent.futures.as_completed(futures_list):
              print(v.result())
      

      future.result()
      ,它是一种阻塞方法。或者如何打印它?您仍然在按顺序执行它是sraw的目的…我应该如何更改以在这段代码中执行并发执行?我应该等待完成还是停止,然后获得这些结果?