Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the python concurrent.futures module

2025-03-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of how to use the python concurrent.futures module, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe you will gain something after reading this article on how to use the python concurrent.futures module. Let's take a look at it.

Overview

Concurrent.futures is a new module introduced in 3.2. it provides a high-level interface for asynchronous execution of callable objects.

You can use ThreadPoolExecutor for multithreaded programming and ProcessPoolExecutor for multiprocess programming, both of which implement the same interfaces defined by the abstract class Executor.

This module provides two major types, one is the executor class Executor, and the other is the Future class.

Executors are used to manage work pools, and future is used to manage the results of work calculations, usually without directly manipulating future objects, because there is a wealth of API.

Description

At the beginning of Python3.2, the standard library provides us with concurrent.futures module, which provides two classes: ThreadPoolExecutor and ProcessPoolExecutor, realizes the further abstraction of threading and multiprocessing, and provides direct support for writing thread pool / process pool.

#! / usr/bin/env python#-*-coding: utf-8-*-# #-# Name: demo3# Author: yunhgu # Date: 2021-7-8 15 import osimport timeimport threadingfrom concurrent.futures import ProcessPoolExecutor Description: #-import osimport timeimport threadingfrom concurrent.futures import ProcessPoolExecutor ThreadPoolExecutor, as_completeddef work (x): time.sleep (1) temp = f "parent process {os.getppid ()}: child process {os.getpid ()}: thread {threading.get_ident ()}: {x}" return tempdef sub_thread (): temp_list = [] with ThreadPoolExecutor (max_workers=3) as t: task_list = [t.submit (work) I) for i in range (5)] for task in as_completed (task_list): if task.done (): temp_list.append (task.result ()) return temp_listdef main (): print (f "main process: {os.getpid ()}") path_list = [] with ProcessPoolExecutor (max_workers=3) as p: task_list = [p.submit (sub_thread) for i in range (5)] for task in as_completed (task_list): if task.done (): path_list.append (task.result ()) for path in path_list: print (path) if _ _ name__ = "_ _ main__": main ()

This is the end of the article on "how to use the python concurrent.futures module". Thank you for reading! I believe you all have a certain understanding of the knowledge of "how to use the python concurrent.futures module". If you want to learn more, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report