Techno Blender
Digitally Yours.
Browsing Tag

asyncio

Combining Traditional Thread-Based Code and Asyncio in Python | by Peng Qian | May, 2023

There is another case where our program already implements a loop in the existing code. For example, most GUI programs use an event loop to respond to various events and to update the UI.Let’s take tkinter as an example. tkinter will start a main loop when it starts, and this main loop will block the main thread and keep on looping. As shown in the figure below:How does the tkinter main loop work. Image by AuthorA direct call to synchronous IO code will block the main loopLet’s take the example of a tkinter program that…

Harnessing Multi-Core Power with Asyncio in Python | by Peng Qian | May, 2023

Boost your Python application’s performance by efficiently utilizing multiple CPU cores with asyncioPhoto Credit: Created by Author, CanvaThis is one of my articles under the Python Concurrency column, and if you find it useful, you can read the rest from here.In this article, I will show you how to execute Python asyncio code on a multi-core CPU to unlock the full performance of concurrent tasks.What is our problem?asyncio uses only one core.In previous articles, I covered the mechanics of using Python asyncio in detail.…

Combining Multiprocessing and Asyncio in Python for Performance Boosts | by Peng Qian | May, 2023

Thanks to GIL, using multiple threads to perform CPU-bound tasks has never been an option. With the popularity of multicore CPUs, Python offers a multiprocessing solution to perform CPU-bound tasks. But until now, there were still some problems with using multiprocess-related APIs directly.Before we start, we still have a small piece of code to aid in the demonstration:The method takes one argument and starts accumulating from 0 to this argument. Print the method execution time and return the result.Problems with…

Why Taskgroup and timeout Are so Crucial in Python 3.11 Asyncio | Structured Concurrency

New features of the Python 3.11 asyncio package1. IntroductionFor every data scientist, improving Python code’s efficiency is essential. Two ideas can help us achieve this goal:Due to the existence of GIL in Python, multi-threading has never been as efficient as expected, and each thread switch needs to compete for GIL locks leading to a severe waste of resources.So, starting with version 3.4, Python introduced a new approach to concurrent programming: coroutine. Without GIL, the coroutine puts the timing of background…

Introduction to asyncio. Managing I/O bound concurrency with… | by Oliver S | Mar, 2023

Managing I/O bound concurrency with PythonConcurrency and parallelism denote a program’s or computer’s capability to run multiple operations in parallel. One commonly distinguishes multi-processing (parallelism) and multi-threading (concurrency)— with the former describing running multiple processes, whereas the second denotes spawning multiple threads within the same process. In Python, due to the Global Interpreter Lock (GIL), only one thread can be executed at once, causing any multi-threaded application to be single…

How to create telnet client with asyncio in Python

Telnet is a client/server application protocol that uses TCP/IP for connection. Telnet protocol enables a user to log onto and use a remote computer as though they were connected directly to it within the local network. The system that is being used by the user for the connection is the client and the remote computer being connected is the server. The commands entered on the terminal in a Telnet client are executed on the server (the remote computer) and the output of the command is directed to the client computer’s…

Understand async/await with asyncio for Asynchronous Programming in Python | by Lynn Kwong | Dec, 2022

Get your hands dirty with a new way of writing asynchronous codeImage by Patrick Hendry in UnsplashMost Python developers may have only worked with synchronous code in Python, even some veteran Pythonistas. However, if you are a data scientist, you may have used the multiprocessing library to run some calculations in parallel. And if you are a web developer, you may have the chance to achieve concurrency with threading. Both multiprocessing and threading are advanced concepts in Python and have their own specific fields…