The AI Book
    Facebook Twitter Instagram
    The AI BookThe AI Book
    • Home
    • Categories
      • AI Media Processing
      • AI Language processing (NLP)
      • AI Marketing
      • AI Business Applications
    • Guides
    • Contact
    Subscribe
    Facebook Twitter Instagram
    The AI Book
    AI Media Processing

    Harnessing the power of multi-core with Asyncio in Python | by Peng Qian | May 2023

    9 May 2023No Comments2 Mins Read

    [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:

    All cores are almost used.
    All cores are almost used. Image by author

    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!

    Almost three times the performance!
    Almost three times the performance! Image by author

    [ad_2]

    Source link

    Previous ArticleResearchers create tool for accurate simulation of complex systems | MIT News
    Next Article Creative Intelligence for Influencer Marketing Delivers Higher ROI
    The AI Book

    Related Posts

    AI Media Processing

    A new set of Arctic images will help artificial intelligence research MIT News

    25 July 2023
    AI Media Processing

    Analyzing rodent infestations using the geospatial capabilities of Amazon SageMaker

    24 July 2023
    AI Media Processing

    Using knowledge of social context for responsible use of artificial intelligence – Google Research Blog

    23 July 2023
    Add A Comment

    Leave A Reply Cancel Reply

    • Privacy Policy
    • Terms and Conditions
    • About Us
    • Contact Form
    © 2025 The AI Book.

    Type above and press Enter to search. Press Esc to cancel.