Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/AsyncPipeline.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import asyncio
from concurrent.futures import ThreadPoolExecutor

class AsyncPipeline(object):
""" Async wrapper for a mpipe pipeline that allows for concurrent streams in
and out via generator functions """

def __init__(self, pipeline, data_source):
self._pipeline = pipeline
self._data_source = data_source
self._executor = ThreadPoolExecutor(2)

async def _pipe_filler(self):
""" Async task to pass data from _data_source generator function into
pipeline """
for item in self._data_source():
await self._loop.run_in_executor(
self._executor,
lambda: self._pipeline.put(item)
)

self._pipeline.put(None)

async def start(self):
""" Async generator method which starts the input stream into the
pipeline and yields result values from the pipeline """
self._loop = asyncio.get_event_loop()
self._loop.create_task(self._pipe_filler())
while True:
result = await self._loop.run_in_executor(
self._executor,
self._pipeline.get
)
if result is None: break
yield result
1 change: 1 addition & 0 deletions src/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@
from .OrderedStage import OrderedStage
from .UnorderedStage import UnorderedStage
from .Pipeline import Pipeline
from .AsyncPipeline import AsyncPipeline
from .FilterWorker import FilterWorker
from .FilterStage import FilterStage