-
Notifications
You must be signed in to change notification settings - Fork 1
Add processor for concurrent execution of function #283
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c7eea97
Add processor for concurrent execution of function
Tomperez98 44d54fb
use callback and shutdown queue
Tomperez98 63c6119
back to use none
Tomperez98 107b30c
Join queue
Tomperez98 c0d7bb5
ignore rule
Tomperez98 06a38be
allow workers to be configurable
Tomperez98 245916f
Add tests for the processor
Tomperez98 f485149
back to bind variables
Tomperez98 d490783
Add test for the processor and remove join on queue
Tomperez98 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
from __future__ import annotations | ||
|
||
import os | ||
import queue | ||
from collections.abc import Callable | ||
from threading import Thread | ||
from typing import Any | ||
|
||
from resonate.models.result import Ko, Ok, Result | ||
from resonate.utils import exit_on_exception | ||
|
||
|
||
class Processor: | ||
def __init__(self, workers: int | None = None) -> None: | ||
self.threads = set[Thread]() | ||
for _ in range(min(32, workers or (os.cpu_count() or 1))): | ||
self.threads.add(Thread(target=self._run, daemon=True)) | ||
|
||
self.sq = queue.Queue[tuple[Callable[[], Any], Callable[[Result[Any]], None]] | None]() | ||
|
||
@exit_on_exception | ||
def _run(self) -> None: | ||
while sqe := self.sq.get(): | ||
func, callback = sqe | ||
|
||
try: | ||
r = Ok(func()) | ||
except Exception as e: | ||
r = Ko(e) | ||
|
||
callback(r) | ||
self.sq.task_done() | ||
|
||
self.sq.task_done() | ||
|
||
def enqueue(self, func: Callable[[], Any], callback: Callable[[Result[Any]], None]) -> None: | ||
self.sq.put((func, callback)) | ||
|
||
def start(self) -> None: | ||
for t in self.threads: | ||
if not t.is_alive(): | ||
t.start() | ||
|
||
def stop(self) -> None: | ||
for _ in self.threads: | ||
self.sq.put(None) | ||
|
||
self.sq.join() | ||
|
||
for t in self.threads: | ||
t.join() | ||
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
from __future__ import annotations | ||
|
||
from queue import Queue | ||
|
||
import pytest | ||
|
||
from resonate.models.result import Ok, Result | ||
from resonate.processor import Processor | ||
|
||
|
||
def greet(name: str) -> str: | ||
return f"Hi {name}" | ||
|
||
|
||
def callback(q: Queue[tuple[str, str]], expected: str, result: Result[str]) -> None: | ||
assert isinstance(result, Ok) | ||
q.put((result.value, expected)) | ||
|
||
|
||
@pytest.mark.parametrize("workers", [1, 2, 3]) | ||
def test_processor(workers: int) -> None: | ||
q = Queue[tuple[str, str]]() | ||
p = Processor(workers) | ||
assert len(p.threads) == workers | ||
|
||
names = ["A", "B"] | ||
expected_greet = [greet("A"), greet("B")] | ||
p.start() | ||
for name, expected in zip(names, expected_greet, strict=False): | ||
p.enqueue(lambda: greet(name), lambda r: callback(q, expected, r)) | ||
|
||
p.stop() | ||
assert q.qsize() == len(names) | ||
|
||
for _ in range(q.qsize()): | ||
actual, expected = q.get() | ||
assert actual == expected | ||
q.task_done() | ||
|
||
q.join() | ||
assert q.empty() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.