'How to set all function in python run async

Is a any way, all function - builtin or user defined - in Python's module run async. For example in this code sum, print, f and ..., all of them run async not sync. Is Python have setting for this?

a = sum([1, 2, 3, 4])
print(a)

def f():
    print("Example")
f()


Solution 1:[1]

They do not run asynchronously. To make code run asynchronously, you can use the asyncio package and add the keyword async to the front of your method:

import asyncio

async def sum():
    a = sum(1, 2, 3, 4)
    print(a)

async def f():
    print("Example")

asyncio.run(sum())
asyncio.run(f())

You can now, not rely on either function to run "first". Both are async and are running in separate threads. You also have a syntax error at the end of your print statement.

Docs: https://docs.python.org/3/library/asyncio-task.html

Note: If you know Lua or you are aware of what coroutines and tasks are and how they play a role in development, then the docs above should be easy enough to understand.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1