Skip to content

Commit

Permalink
feat: implement daemon mode
Browse files Browse the repository at this point in the history
  • Loading branch information
pikann committed Dec 8, 2023
1 parent 2f5a0ff commit 9a8d53c
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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,
)
Expand Down
1 change: 1 addition & 0 deletions examples/lazy_mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions examples/subscribe.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "sqs-client"
version = "0.1.1"
version = "0.1.2"
authors = [
{name="Digital Fortress", email="[email protected]" },
]
Expand Down
4 changes: 4 additions & 0 deletions sqs_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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")
Expand All @@ -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
Expand Down
8 changes: 7 additions & 1 deletion sqs_client/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def __init__(
wait_time_seconds=20,
delay_seconds=0,
lazy=False,
daemon=True,
):
"""
Initializes the Task class.
Expand All @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 9a8d53c

Please sign in to comment.