I am working on a script to introduce a non blocking async fire and forget call. Thought of using the asyncio for that. I am running a below POC code before get into a full fledge implementation.It appears that the async function is still running on the main thread. Why is the below async function not creating a new thread name/id? I am expecting a new thread id like the threading module generates.
import asyncio
import threading
from threading import Thread
#Asyncio function
async def async_func():
print(f'async_func thread name: %s', threading.current_thread().name)
print(f'async_func thread id: %s', threading.current_thread().ident)
await asyncio.sleep(1)
# Threading
def non_async_func():
print(f'non_async_func thread name: %s', threading.current_thread().name)
print(f'non_async_func thread id: %s', threading.current_thread().ident)
def main():
print(f'main name: %s', threading.current_thread().name)
print(f'main id: %s', threading.current_thread().ident)
asyncio.run(async_func())
t = Thread(target=non_async_func)
print(f'main thread name: %s', threading.current_thread().name)
print(f'main thread id: %s', threading.current_thread().ident)
t.start()
if __name__ == '__main__':
main()
I am getting the below output:
main name: %s **MainThread**
main id: %s **4691527168**
**async_func thread name: %s MainThread**
**async_func thread id: %s 4691527168**
main thread name: %s **MainThread**
main thread id: %s **4691527168**
non_async_func thread name: %s **Thread-1 (non_async_func)**
non_async_func thread id: %s **123145474224128**