How do you enable parallel processing in python? Threading is one way, but building a fully resilient multi-threaded process is hard. Incorporating asynchronous processing in python is a much simpler and practical way to introduce parallel processing in your applications. In this very short blog post, you will learn how to enable simple async functionalities.

The “Normal” Way

Let’s examine the standard way a program is written. Consider a process defined in a function called myproc() that does something that takes 5 seconds. We will simulate it by putting a sleep of 5 seconds. When the function call is made, we print a line “myproc started …”. Likewise, we print “myproc finished …” when the function finished execution. Using the time module, we will record the elapsed time. Here is the code.

import time

def myproc():
    print("myProc started ...")
    t1 = time.perf_counter()
    time.sleep(5)
    t = time.perf_counter() - t1
    print(f"   myProc finished in {t:0.5f} seconds.")

def main():
    for _ in range(5):
        myproc()

if __name__ == "__main__":
    start_sec = time.perf_counter()
    main()
    elapsed_secs = time.perf_counter() - start_sec
    print(f"Job finished in {elapsed_secs:0.5f} seconds.")

#data-engineering #python #await #asynchronous-programming #asyncio

Async Processing in Python
2.95 GEEK