[ad_1]
Next, let’s implement the multi-core asynchronous code according to the flowchart and see if the performance is improved.
Development of the overall structure of the code
First, as an architect, we still need to define the overall structure of the script, what methods are needed, and what tasks each method should perform:
A specific implementation of each method
Next, let’s implement each method step by step.
The query_concurrently
The method will simultaneously start the specified batch of tasks and receive the results asyncio.gather
Method:
The run_batch_tasks
The method is not an asynchronous method because it starts directly in the child process:
And finally, there is ours main
method. This method calls loop.run_in_executor
method to have run_batch_tasks
Executing the method in the process pool and concatenating the child process execution results into a list:
Since we are writing a multi-process script, we need to use if __name__ == “__main__”
To start the main method in the main process:
Run the code and see the results
Next, we start the script and look at the load on each core in the task manager:
As you can see, all CPU cores are in use.
Finally, we observe the execution time of the code and confirm that multi-threaded asynchronous code really speeds up code execution several times! Mission complete!
[ad_2]
Source link