SpringMaple
SpringMaple

Reputation: 309

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?

Upvotes: 4

Views: 1013

Answers (1)

Vincent
Vincent

Reputation: 13415

Simply use loop.call_soon_threadsafe to schedule loop.call_later:

loop.call_soon_threadsafe(loop.call_later, 1, print, 'bomb')

Upvotes: 10

Related Questions