Skip to content
This repository has been archived by the owner on Apr 7, 2022. It is now read-only.

Commit

Permalink
Fix asyncio executor on the server side
Browse files Browse the repository at this point in the history
  • Loading branch information
Vincent Michel committed Jan 16, 2017
1 parent 9fa2505 commit 995db76
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions tango/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -1616,6 +1616,7 @@ def stop(self):

def _create_asyncio_worker():
import concurrent.futures
from threading import get_ident

try:
import asyncio
Expand All @@ -1635,11 +1636,10 @@ def __init__(self, loop=None):
"""Initialize the executor with a given loop."""
self.loop = loop or asyncio.get_event_loop()

def submit(self, fn, *args, **kwargs):
"""Schedule the callable fn, to be executed as fn(*args **kwargs).
def submit(self, corofn, *args, **kwargs):
"""Schedule a coroutine, to be executed as corofn(*args **kwargs).
Return a Future representing the execution of the callable."""
corofn = asyncio.coroutine(lambda: fn(*args, **kwargs))
return run_coroutine_threadsafe(corofn(), loop)
return run_coroutine_threadsafe(corofn(*args, **kwargs), self.loop)

def run_in_thread(self, func, *args, **kwargs):
"""Schedule a blocking callback."""
Expand All @@ -1664,7 +1664,10 @@ def stop(self):

def execute(self, fn, *args, **kwargs):
"""Execute the callable fn as fn(*args **kwargs)."""
return self.submit(fn, *args, **kwargs).result()
corofn = asyncio.coroutine(fn)
if self.loop._thread_id == get_ident():
return corofn(*args, **kwargs)
return self.submit(corofn, *args, **kwargs).result()

try:
loop = asyncio.get_event_loop()
Expand Down

0 comments on commit 995db76

Please sign in to comment.