欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程语言 > python >内容正文

python

Python并发编程之concurrent.futures

发布时间:2023/12/10 python 49 豆豆
生活随笔 收集整理的这篇文章主要介绍了 Python并发编程之concurrent.futures 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

2019独角兽企业重金招聘Python工程师标准>>>

    concurrent.futures模块提供了一个异步执行callables的高级接口。 可以使用ThreadPoolExecutor和ProcessPoolExecutor。 两者都继承了相同的接口,该接口由抽象的Executor类定义。

    一个抽象类,提供异步执行调用的方法。 它不应该直接使用,而是通过其具体的子类。

    submit (fn, *args, **kwargs):提交执行的函数并获取一个Future对象,例如:

with ThreadPoolExecutor(max_workers=1) as executor:future = executor.submit(pow, 323, 1235)print(future.result())

    ThreadPoolExecutor:是Executor子类,它使用一个线程池来异步执行调用。concurrent.futures.ThreadPoolExecutor(max_workers=None, thread_name_prefix='', initializer=None, initargs=())。例如:

import concurrent.futures import urllib.request URLS = ['http://www.foxnews.com/','http://www.cnn.com/','http://europe.wsj.com/','http://www.bbc.co.uk/','http://some-made-up-domain.com/']# Retrieve a single page and report the URL and contents def load_url(url, timeout):with urllib.request.urlopen(url, timeout=timeout) as conn:return conn.read()# We can use a with statement to ensure threads are cleaned up promptly with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:# Start the load operations and mark each future with its URLfuture_to_url = {executor.submit(load_url, url, 60): url for url in URLS}for future in concurrent.futures.as_completed(future_to_url):url = future_to_url[future]try:data = future.result()except Exception as exc:print('%r generated an exception: %s' % (url, exc))else:print('%r page is %d bytes' % (url, len(data)))

    ProcessPoolExecutor:concurrent.futures.ProcessPoolExecutor(max_workers=None, mp_context=None, initializer=None, initargs=()):使用如下:

import concurrent.futures import math executor = ProcessPoolExecutor(max_workers=5)def is_prime(n):if n % 2 == 0:return Falsesqrt_n = int(math.floor(math.sqrt(n)))for i in range(3, sqrt_n + 1, 2):if n % i == 0:return Falsereturn Truedef main():for i in range(10):         future = executor.submit(is_prime, n)if __name__ == '__main__':main()

    Future: Future类封装了可调用的异步执行。 该实例由Executor.submit()创建。

转载于:https://my.oschina.net/u/3316387/blog/2996178

总结

以上是生活随笔为你收集整理的Python并发编程之concurrent.futures的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。