Created
May 31, 2016 21:23
-
-
Save SimplyAhmazing/b69303dd63f02a6cb33c184a4608f2b9 to your computer and use it in GitHub Desktop.
This file contains 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 multiprocessing | |
import os | |
from concurrent.futures import ThreadPoolExecutor | |
@asyncio.coroutine | |
def coro_get(q): | |
loop = asyncio.get_event_loop() | |
return (yield from loop.run_in_executor(ThreadPoolExecutor(3), q.get)) | |
@asyncio.coroutine | |
def coro_put(q, val): | |
loop = asyncio.get_event_loop() | |
return (yield from loop.run_in_executor(ThreadPoolExecutor(3), q.put_nowait, val)) | |
@asyncio.coroutine | |
def producer(q): | |
n = 0 | |
while True: | |
n += 1 | |
print('now putting:', n) | |
yield from coro_put(q, n) | |
yield from asyncio.sleep(1) | |
@asyncio.coroutine | |
def consumer(q): | |
while True: | |
res = yield from coro_get(q) | |
print('Consumed:', res) | |
def wrap_producer(q): | |
print('Starting producer in process id: ', os.getpid(), os.getppid()) | |
loop = asyncio.get_event_loop() | |
loop.run_until_complete(producer(q)) | |
def wrap_consumer(q): | |
print('Starting consumer in process id: ', os.getpid(), os.getppid()) | |
loop = asyncio.get_event_loop() | |
loop.run_until_complete(consumer(q)) | |
# qq = asyncio.Queue() | |
# qq = multiprocessing.Queue() | |
qq = multiprocessing.Manager().Queue(50) | |
p = multiprocessing.Process(target=wrap_producer, args=(qq,)) | |
c = multiprocessing.Process(target=wrap_consumer, args=(qq,)) | |
p.start() | |
c.start() | |
p.join() | |
c.join() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment