In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to run the cooperative program in the thread of Python". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to run the collaborative program in the thread of Python".
Is there any way to make synchronous and asynchronous code seem to run at the same time? The method uses the. run_in_executor () method of the event loop.
Let's take a look at the statement in the Python official document [1]:
So how do you use it? Or take this function of the Fibonacci series in a very time-consuming recursive way as an example:
Def sync_calc_fib (n): if n in [1,2]: return1 return sync_calc_fib (n-1) + sync_calc_fib (n-2) async def calc_fib (n): result = sync_calc_fib (n) print (f 'item {n}) the result is: {result}') return result
We now need to use aiohttp to access a web page with a delay of 5 seconds and calculate item 36 of the Fibonacci series.
First of all, let's take a look at the 5 seconds it takes to calculate item 36 separately:
Let's take a look at the fact that if we directly put the calculation of the Fibonacci sequence and the two asynchronous tasks of the requesting website "in parallel", the actual time is the time superposition of the two tasks:
I have explained the specific reasons in the last article.
Now, I want two tasks to "run at the same time", so I can modify the code like this:
Import aiohttp import asyncio import time from concurrent.futures import ThreadPoolExecutor async def request (sleep_time): async with aiohttp.ClientSession () as client: resp = await client.get (f 'http://127.0.0.1:8000/sleep/{sleep_time}') resp_json = await resp.json () print (resp_json) def sync_calc_fib (n): if n in [1 2]: return 1 return sync_calc_fib (n-1) + sync_calc_fib (n-2) def calc_fib (n): result = sync_calc_fib (n) print (f 'item {n}) completed The result is: {result}') return result async def main (): start = time.perf_counter () loop = asyncio.get_event_loop () with ThreadPoolExecutor (max_workers=4) as executor: tasks_list = [loop.run_in_executor (executor, calc_fib, 36) Asyncio.create_task (request (5))] await asyncio.gather (* tasks_list) end = time.perf_counter () print (total time: {end-start}') asyncio.run (main ())
The running effect is shown in the following figure:
In 5 seconds, the site that calculates the Fibonacci series and requests a 5-second delay is done.
To achieve this transformation, the key code is: loop.run_in_executor (executor, calc_fib, 36)
The loop is the event loop (event loop) of the main thread, which is used to schedule multiple collaborators in the same thread.
Executor is a pool of 4 threads that we created using ThreadPoolExecutor (max_workers=4), calc_fib is a time-consuming synchronization function, and 36 is a parameter passed in to calc_fib. Loop.run_in_executor (executor, calc_fib, 36) means:
Put the calc_fib function into the thread pool to run
Add a callback function to the thread pool that saves the result in the next event loop after the end of the run.
Notice the calc_fib corresponding to the red arrow in the figure above. This is a synchronous function, which should be distinguished from the asynchronous function in the previous article. The second argument to run_in_executor needs to be the name of a synchronization function.
Thank you for your reading, the above is the content of "how to run the cooperative program in the thread of Python". After the study of this article, I believe you have a deeper understanding of how to run the cooperative program in the thread of Python. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.