-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make pynbody server work asynchronously so it doesn't block the serve…
…r from processing other quick messages
- Loading branch information
Showing
5 changed files
with
91 additions
and
17 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import queue | ||
from threading import Thread | ||
|
||
from . import on_exit_parallelism | ||
from .message import Message | ||
|
||
|
||
class AsyncProcessedMessage(Message): | ||
_async_task_queue = queue.Queue() | ||
def process_async(self): | ||
"""Override to provide the processing/response mechanism, that will be performed in a separate thread""" | ||
raise NotImplementedError() | ||
|
||
def process(self): | ||
self._async_task_queue.put(self) | ||
|
||
def init_async_processing_thread(): | ||
def async_processing_thread(): | ||
while True: | ||
msg = AsyncProcessedMessage._async_task_queue.get() | ||
if msg is None: | ||
break | ||
msg.process_async() | ||
|
||
t = Thread(target=async_processing_thread) | ||
t.daemon = True | ||
t.start() | ||
|
||
def exit_async_processing_thread(): | ||
AsyncProcessedMessage._async_task_queue.empty() | ||
AsyncProcessedMessage._async_task_queue.put(None) | ||
t.join() | ||
|
||
on_exit_parallelism(exit_async_processing_thread) |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import time | ||
|
||
from tangos import parallel_tasks as pt | ||
from tangos.parallel_tasks import async_message, message, testing | ||
|
||
|
||
class Response(message.Message): | ||
pass | ||
|
||
|
||
class SlowProcessingMessage(async_message.AsyncProcessedMessage): | ||
def process_async(self): | ||
time.sleep(0.1) | ||
Response("slow").send(self.source) | ||
|
||
class FastProcessingMessage(message.Message): | ||
def process(self): | ||
Response("fast").send(self.source) | ||
|
||
def _test_async_message(): | ||
SlowProcessingMessage().send(0) | ||
FastProcessingMessage().send(0) | ||
msg = Response.receive(0) | ||
# fast message response should overtake slow message response | ||
assert msg.contents == "fast" | ||
|
||
msg = Response.receive(0) | ||
assert msg.contents == "slow" | ||
|
||
|
||
|
||
def test_async_message(): | ||
pt.use('multiprocessing-2') | ||
pt.launch(_test_async_message) |