PyKron is a open-source package that let you easily transform any Python function in an asynchronous call.
- To make a def statement an asynchronous call
The original code contains two def statements as shown below. The execution will take 4+2 seconds, since foo2 is executed right after foo2.
def foo1():
time.sleep(4)
print("foo1")
def foo2():
time.sleep(2)
print("foo2")
foo1()
foo2()
Now we want to make foo1 and foo2 asynchronous using PyKron. What we need to do is basically wrap them with specific decorators of the AsyncRequest class. As shown below, the execution will take now only 4 seconds cause foo1 and foo2 are launched in parallel.
from pykron.core import Pykron
app = Pykron()
@app.AsyncRequest()
def foo1():
time.sleep(4)
print("foo1")
@app.AsyncRequest()
def foo2():
time.sleep(2)
print("foo2")
foo1.asyn()
foo2.asyn()