Last active
June 7, 2018 09:57
-
-
Save TennyZhuang/5485eb3e787c4d59468babf97b339185 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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