Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why no call_at_threadsafe and call_later_threadsafe?

I'm using Python 3.5.2 in Windows 32bits and aware that asyncio call_at is not threadsafe, hence following code won't print 'bomb' unless I uncomment the line loop._write_to_self().

import asyncio
import threading


def bomb(loop):
    loop.call_later(1, print, 'bomb')
    print('submitted')
    # loop._write_to_self()


if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    threading.Timer(2, bomb, args=(loop,)).start()
    loop.run_forever()

However I couldn't find a piece of information about why call_at_threadsafe and call_later_threadsafe is implemented. Is the reason ever exists?

like image 449
SpringMaple Avatar asked Aug 30 '25 18:08

SpringMaple


1 Answers

Simply use loop.call_soon_threadsafe to schedule loop.call_later:

loop.call_soon_threadsafe(loop.call_later, 1, print, 'bomb')
like image 153
Vincent Avatar answered Sep 07 '25 19:09

Vincent