8

I'm trying to use asyncio in my Django post processing like:

query : # a query to my model tasks = [] for record in query: tasks.append(do_something_with_google_calendar(record)) loop = asyncio.get_event_loop() loop.run_until_complete(asyncio.wait(tasks)) loop.close() 

But I just get an error while executing:

loop = asyncio.get_event_loop() RuntimeError: There is no current event loop in thread 'Thread-17'. 

Any ideas?

Thank you in advance

5
  • How is defined the method do_something_with_google_calendar(record) Commented Jan 11, 2017 at 15:21
  • @asyncio.coroutine def do_something_with_google_calendar( args ): Commented Jan 11, 2017 at 16:02
  • 2
    Why not use something like Celery for post-processing? AsyncIO is not meant for post-processing, it's meant to be used with non-blocking IO. Commented Jan 11, 2017 at 16:10
  • I start to realize this. Commented Jan 11, 2017 at 21:41
  • 1
    I thought I could manage to access in an asynchronous way the Google Calendar. I use pythonanywhere server. I cannot use Celery there. I will probably have to change to a new server. Commented Jan 11, 2017 at 21:52

2 Answers 2

7

Your original code woun't work since get_event_loop() method is only shortcut to get_event_loop_policy().get_event_loop() which create&return event loop automatically for main thread only. To make it work right you need to explicitly create and set new event loop for each current thread context:

query : # a query to my model tasks = [] for record in query: tasks.append(do_something_with_google_calendar(record)) loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) loop.run_until_complete(asyncio.wait(tasks)) loop.close() 

You may read more about this stuff here.

Sign up to request clarification or add additional context in comments.

Comments

2

It looks that if I do like this, it works:

query : # a query to my model tasks = [] for record in query: tasks.append(do_something_with_google_calendar(record)) loop = asyncio.SelectorEventLoop() asyncio.set_event_loop(loop) loop.run_until_complete(asyncio.wait(tasks)) loop.close() 

I hope it is stable and it will also work fine in UNIX as it does in Windows

1 Comment

Each separate thread expects its own asyncio event-loop since each loop takes over the thread's execution and schedules the async functions/ coroutines for it; therefore you can use asyncio.new_event_loop() to get a new event loop once in each thread, and then get_event_loop() will return the loop defined for that thread. (Your solution should work as well since SelectorEventLoop will return a new loop as well)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.