0

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**
Paul Cornelius
  • 9,245
  • 1
  • 15
  • 24
GAK
  • 1,018
  • 1
  • 14
  • 33
  • 3
    Of course it's on the same thread, that's what asyncio is. You already found out how to create new threads if you want new threads, that's the threading module. – Dan Getz Dec 27 '22 at 03:42
  • Maybe you could describe what you're really looking to do in your function -- **what** is it you want to "fire and forget"? I wonder if you're looking for the executors in [concurrent.futures](https://docs.python.org/3/library/concurrent.futures.html#module-concurrent.futures)? – Dan Getz Dec 27 '22 at 04:01
  • the fire and forget should be an async service call over http. I don't want to block the main thread wait for the response from the http service. – GAK Dec 27 '22 at 04:24
  • Then asyncio should be fine. I don't see why it would matter what thread the http call is handled on. – Dan Getz Dec 27 '22 at 12:23
  • I dont want my main thread to block until it receives a response or a timeout from the http service call. Thats the reason why I want to spin up a new thread. – GAK Dec 27 '22 at 18:21
  • Asyncio is made for that, even though it doesn't use threads. As long as your http library uses asyncio, it won't block the main thread: the I/O will be handled in an async way, hence the name. If your http library is synchronous instead, then you would need threads instead of asyncio. – Dan Getz Dec 27 '22 at 20:51
  • Does this answer your question? [How does asyncio actually work?](https://stackoverflow.com/questions/49005651/how-does-asyncio-actually-work) – Dan Getz Dec 31 '22 at 21:46

0 Answers0