diff --git a/README.md b/README.md index 288eb3f..6ce38f4 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,7 @@ sqs_client = SQSClient() queue_name="sqs-queue-name", wait_time_seconds=0, visibility_timeout=300, + daemon=False, ) def test_task(message): print("test_task received:", message) @@ -74,6 +75,7 @@ sqs_client = SQSClient() @sqs_client.task( queue_name="sqs-queue-name", lazy=True, + daemon=False, wait_time_seconds=0, visibility_timeout=300, ) diff --git a/examples/lazy_mode.py b/examples/lazy_mode.py index edc3f8f..dc4f981 100644 --- a/examples/lazy_mode.py +++ b/examples/lazy_mode.py @@ -9,6 +9,7 @@ lazy=True, wait_time_seconds=0, visibility_timeout=300, + daemon=False, ) def test_task(message, abc): print("test_task received message:", message) diff --git a/examples/subscribe.py b/examples/subscribe.py index 22f6ff2..36b3e43 100644 --- a/examples/subscribe.py +++ b/examples/subscribe.py @@ -8,6 +8,7 @@ queue_name="sqs-queue-name", wait_time_seconds=0, visibility_timeout=300, + daemon=False, ) def test_task(message): print("test_task received:", message) diff --git a/pyproject.toml b/pyproject.toml index 28a1b60..6fb4f62 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "sqs-client" -version = "0.1.1" +version = "0.1.2" authors = [ {name="Digital Fortress", email="hai.huynh@digitalfortress.dev" }, ] diff --git a/sqs_client/client.py b/sqs_client/client.py index d867a16..4aae9d5 100644 --- a/sqs_client/client.py +++ b/sqs_client/client.py @@ -66,6 +66,7 @@ def task( visibility_timeout=30, wait_time_seconds=20, lazy=False, + daemon=True, ): """ Decorator to create a task object out of any callable. @@ -81,6 +82,8 @@ def task( to arrive in the queue before returning. Default: 20. lazy: (bool) Make this task lazy mode. Trigger SQS message by task_name.trigger(*args, **kwargs) + daemon: (bool) Make this task daemon mode. + The entire Python program exits when no alive non-daemon threads are left. Examples: @sqs_client.task(queue="dev-retailer_getting_order_sqs") @@ -100,6 +103,7 @@ def inner_create_task(callback): visibility_timeout=visibility_timeout, wait_time_seconds=wait_time_seconds, lazy=lazy, + daemon=daemon, ) self._task_list[task.get_id()] = task return task diff --git a/sqs_client/task.py b/sqs_client/task.py index 10991b0..dce5a25 100644 --- a/sqs_client/task.py +++ b/sqs_client/task.py @@ -20,6 +20,7 @@ def __init__( wait_time_seconds=20, delay_seconds=0, lazy=False, + daemon=True, ): """ Initializes the Task class. @@ -40,6 +41,8 @@ def __init__( when use trigger function. Valid values: 0 to 900. Default: 0 lazy: (bool) Make this task lazy mode. Trigger SQS message by task_name.trigger(*args, **kwargs) + daemon: (bool) Make this task daemon mode. + The entire Python program exits when no alive non-daemon threads are left. """ self._id = str(uuid.uuid4()) self._sqs_client = sqs_client @@ -50,6 +53,7 @@ def __init__( self._wait_time_seconds = wait_time_seconds self._delay_seconds = delay_seconds self._lazy = lazy + self._daemon = daemon self._thread = self._create_subscribe_thread() self._publisher = Publisher( sqs_client=self._sqs_client, @@ -106,7 +110,9 @@ def _create_subscribe_thread(self): Returns: (Thread) The asynchronous thread to continuously receive messages from the SQS queue """ - thread = threading.Thread(target=self.subscribe, name=self._id) + thread = threading.Thread( + target=self.subscribe, name=self._id, daemon=self._daemon + ) thread.start() return thread