8

I'm trying to use a coroutine function outside of the event loop. (In this case, I want to call a function in Django that could also be used inside the event loop too)

There doesn't seem to be a way to do this without making the calling function a coroutine.

I realize that Django is built to be blocking and a therefore incompatible with asyncio. Though I think that this question might help people who are making the transition or using legacy code.

For that matter, it might help to understand async programming and why it doesn't work with blocking code.

2
  • Even if you push Django WSGI request handling into asyncio event loop it's synchronous call by definition -- and bad idea in general. Commented May 29, 2014 at 9:05
  • It's hard to let go of Django, where it provides you so much Commented May 29, 2014 at 15:49

2 Answers 2

2

After a lot of research I think these solutions could be helpful:

  1. Update your legacy code to use asyncio:

    Yes i know that it can be hard and painful, but it might be the sanest choice. If you are wanting to use Django like I was... Well, you've got a lot of work to do to make Django async'd. I'm not sure it is possible, but I found at least one attempt: https://github.com/aaugustin/django-c10k-demo (Though, in a youtube video the author explained all the shortcomings of this).

  2. use asyncio.async or asyncio.Task:

    These items will enable you to run something async inside of blocking code, but the downfall here is that you will not be able to wait for them to finish without doing something ugly like a while loop that checks if the future has completed... ugh, but if you don't need the result, that might work for you.

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

1 Comment

There is a django-channels project that could help anyone using django to start getting async
2

About case #2: Blocking code should be at least wrapped with .run_in_executor.

2 Comments

Could you provide a code sample and brief explanation?
Suppose you have a "fn" function doing blocking I/O. If you run it from a coroutine, the coroutine will block at least until the IO is completed. Threads in python are great for this and asyncio uses the loop.run_in_executor method to suspend a thread until it finished. loop = asyncio.get_event_loop(); result = await loop.run_in_executor(None, fn, *args)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.