Skip to content

Instantly share code, notes, and snippets.

@TennyZhuang
Last active June 7, 2018 09:57
Show Gist options
  • Save TennyZhuang/5485eb3e787c4d59468babf97b339185 to your computer and use it in GitHub Desktop.
Save TennyZhuang/5485eb3e787c4d59468babf97b339185 to your computer and use it in GitHub Desktop.
import asyncio
import janus
import threading
loop = asyncio.get_event_loop()
q = janus.Queue(loop=loop)
class EventLoopThread(threading.Thread):
def run(self):
print('thread start')
loop.run_until_complete(self.wait_call_from_queue())
async def wait_call_from_queue(self):
while True:
co, sem, ret_placeholder = (await q.async_q.get())
ret_placeholder[0] = await co
sem.release()
async def async_work(plus):
await asyncio.sleep(plus)
return 'ha'
def sync_work(plus):
sem = threading.Semaphore(0)
ret_placeholder = [None]
item = (async_work(plus), sem, ret_placeholder)
q.sync_q.put(item)
sem.acquire()
return ret_placeholder[0]
if __name__ == '__main__':
th2 = EventLoopThread()
th2.start()
print(sync_work(1))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment