Skip to content
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

Fix Thread Blocking in _invoke_callback() (βœ“ Sandbox Passed) #20

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
18 changes: 10 additions & 8 deletions broadcast_service/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@ def _invoke_callback(
) -> Any:
if enable_async:
future_result = thread_pool.submit(callback, *args, **kwargs)
if future_result.result() is not None:
logger.debug(f"[broadcast-service invoke_callback result] {future_result.result()}")
return future_result.result()
def handle_future_result(future):
result = future.result()
if result is not None:
logger.debug(f"[broadcast-service invoke_callback result] {result}")
future_result.add_done_callback(handle_future_result)
else:
return callback(*args, **kwargs)

Expand Down Expand Up @@ -161,7 +163,8 @@ def _invoke_broadcast_topic(self, topic_name: str, *args, **kwargs):

def _final_invoke_listen_callback(self, callback: Callable, *args, **kwargs) -> Any:
self.logger.debug(f"[broadcast-service] {callback.__name__} is called")
return _invoke_callback(callback, self.thread_pool, self.enable_async, *args, **kwargs)
_invoke_callback(callback, self.thread_pool, self.enable_async, *args, **kwargs)
return None

def stop_listen(self, topic_name: str, callback: Callable):
if topic_name not in self.pubsub_channels.keys():
Expand Down Expand Up @@ -375,12 +378,11 @@ def _invoke_broadcast_topic(self, topic_name: str, *args, **kwargs):
f"[broadcast-service] start_publisher_callback_or_not: {self.cur_publisher_dispatch_config.start_publisher_callback_or_not}")
if self.enable_config and self.cur_publisher_dispatch_config.start_publisher_callback_or_not:
self._invoke_finish_callback()
return None

def _final_invoke_listen_callback(self, callback: Callable, *args, **kwargs):
result = super()._final_invoke_listen_callback(callback, *args, **kwargs)

if result:
self.cur_publisher_dispatch_config.append_sub_callback_results(result)
super()._final_invoke_listen_callback(callback, *args, **kwargs)
return None


broadcast_service = BroadcastService()
33 changes: 33 additions & 0 deletions tests/test_invoke_callback.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import unittest
from concurrent.futures import ThreadPoolExecutor

from broadcast_service import BroadcastService


class TestInvokeCallback(unittest.TestCase):
def setUp(self):
self.broadcast_service = BroadcastService()
self.thread_pool = ThreadPoolExecutor(max_workers=5)
self.callback1 = lambda x: x * 2
self.callback2 = lambda x: x + 2

def test_invoke_callback_async(self):
result = self.broadcast_service._invoke_callback(self.callback1, self.thread_pool, True, 5)
self.assertEqual(result, 10)

def test_invoke_callback_sync(self):
result = self.broadcast_service._invoke_callback(self.callback1, self.thread_pool, False, 5)
self.assertEqual(result, 10)

def test_multiple_callbacks(self):
self.broadcast_service.listen('Test', self.callback1)
self.broadcast_service.listen('Test', self.callback2)
self.broadcast_service.broadcast('Test', 5)
self.assertEqual(self.broadcast_service.pubsub_channels['Test'][0](5), 10)
self.assertEqual(self.broadcast_service.pubsub_channels['Test'][1](5), 7)

def tearDown(self):
self.thread_pool.shutdown()

if __name__ == '__main__':
unittest.main()