ltq 0.1.0__py3-none-any.whl → 0.1.2__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
ltq/task.py CHANGED
@@ -1,18 +1,20 @@
1
1
  from __future__ import annotations
2
2
 
3
- from typing import Any, Awaitable, Callable, Generic, ParamSpec
3
+ from functools import update_wrapper
4
+ from typing import Awaitable, Callable, Generic, ParamSpec, TypeVar
4
5
 
5
6
  from .message import Message
6
7
  from .q import Queue
7
8
 
8
9
  P = ParamSpec("P")
10
+ R = TypeVar("R")
9
11
 
10
12
 
11
- class Task(Generic[P]):
13
+ class Task(Generic[P, R]):
12
14
  def __init__(
13
15
  self,
14
16
  name: str,
15
- fn: Callable[P, Awaitable[Any]],
17
+ fn: Callable[P, Awaitable[R]],
16
18
  queue: Queue,
17
19
  ttl: int | None = None,
18
20
  ) -> None:
@@ -36,3 +38,6 @@ class Task(Generic[P]):
36
38
  async def send_bulk(self, messages: list[Message]) -> list[str]:
37
39
  await self.queue.put(messages, ttl=self.ttl)
38
40
  return [message.id for message in messages]
41
+
42
+ async def __call__(self, *args: P.args, **kwargs: P.kwargs) -> R:
43
+ return await self.fn(*args, **kwargs)
ltq/worker.py CHANGED
@@ -3,7 +3,9 @@ from __future__ import annotations
3
3
  import asyncio
4
4
  from functools import partial
5
5
  from pathlib import Path
6
- from typing import TYPE_CHECKING, Any, Awaitable, Callable, ParamSpec
6
+ from typing import TYPE_CHECKING, Any, Awaitable, Callable, ParamSpec, TypeVar
7
+
8
+ import redis.asyncio as redis
7
9
 
8
10
  from .errors import RetryMessage
9
11
  from .task import Task
@@ -19,17 +21,18 @@ logger = get_logger()
19
21
 
20
22
 
21
23
  P = ParamSpec("P")
24
+ R = TypeVar("R")
22
25
 
23
26
 
24
27
  class Worker:
25
28
  def __init__(
26
29
  self,
27
- client: AsyncRedis,
30
+ url: str = "redis://localhost:6379",
28
31
  middlewares: list[Middleware] | None = None,
29
32
  concurrency: int = 250,
30
33
  poll_sleep: float = 0.1,
31
34
  ) -> None:
32
- self.client: AsyncRedis = client
35
+ self.client: AsyncRedis = redis.from_url(url)
33
36
  self.tasks: list[Task] = []
34
37
  self.middlewares: list[Middleware] = middlewares or []
35
38
  self.concurrency: int = concurrency
@@ -39,8 +42,8 @@ class Worker:
39
42
  self,
40
43
  queue_name: str | None = None,
41
44
  ttl: int | None = None,
42
- ) -> Callable[[Callable[P, Awaitable[Any]]], Task[P]]:
43
- def decorator(fn: Callable[P, Awaitable[Any]]) -> Task[P]:
45
+ ) -> Callable[[Callable[P, Awaitable[R]]], Task[P, R]]:
46
+ def decorator(fn: Callable[P, Awaitable[R]]) -> Task[P, R]:
44
47
  filename = Path(fn.__code__.co_filename).stem
45
48
  task_name = f"{filename}:{fn.__qualname__}"
46
49
  queue = Queue(self.client, queue_name or task_name)
@@ -48,7 +51,7 @@ class Worker:
48
51
  name=task_name,
49
52
  fn=fn,
50
53
  queue=queue,
51
- ttl=ttl,
54
+ ttl=ttl,
52
55
  )
53
56
  self.tasks.append(task)
54
57
  return task
@@ -87,5 +90,8 @@ class Worker:
87
90
  await task.queue.ack(messages)
88
91
 
89
92
  async def run(self) -> None:
90
- workers = (self.worker(task) for task in self.tasks)
91
- await asyncio.gather(*workers)
93
+ try:
94
+ workers = (self.worker(task) for task in self.tasks)
95
+ await asyncio.gather(*workers)
96
+ finally:
97
+ await self.client.aclose()
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: ltq
3
- Version: 0.1.0
3
+ Version: 0.1.2
4
4
  Summary: Add your description here
5
5
  Author: Tom Clesius
6
6
  Author-email: Tom Clesius <tomclesius@gmail.com>
@@ -11,7 +11,7 @@ Provides-Extra: sentry
11
11
  Description-Content-Type: text/markdown
12
12
 
13
13
  <p align="center">
14
- <img src="assets/logo.png" alt="LTQ" width="400">
14
+ <img src="https://raw.githubusercontent.com/tclesius/ltq/refs/heads/main/assets/logo.png" alt="LTQ" width="400">
15
15
  </p>
16
16
 
17
17
  <p align="center">
@@ -30,11 +30,9 @@ uv add ltq
30
30
 
31
31
  ```python
32
32
  import asyncio
33
- import redis.asyncio as redis
34
33
  import ltq
35
34
 
36
- client = redis.from_url("redis://localhost:6379")
37
- worker = ltq.Worker(client=client)
35
+ worker = ltq.Worker(url="redis://localhost:6379")
38
36
 
39
37
  @worker.task()
40
38
  async def send_email(to: str, subject: str, body: str) -> None:
@@ -83,7 +81,7 @@ Add middleware to handle cross-cutting concerns:
83
81
  from ltq.middleware import Retry, RateLimit, Timeout
84
82
 
85
83
  worker = ltq.Worker(
86
- client=client,
84
+ url="redis://localhost:6379",
87
85
  middlewares=[
88
86
  Retry(max_retries=3, min_delay=1.0),
89
87
  RateLimit(requests_per_second=10),
@@ -5,9 +5,9 @@ ltq/logger.py,sha256=HPClhDt3ecwZqE0Vq2oYF8Nr9jj-xrsSX9tM6enVgkA,1791
5
5
  ltq/message.py,sha256=C6gJR6KuRrnIb-l9Jna7e-XIu_aFHBhnZICUqE1_2MU,715
6
6
  ltq/middleware.py,sha256=NMRI7UiJiUTqAHOB18Gz686mQGszSu-6hJiyTDPCKdE,3631
7
7
  ltq/q.py,sha256=LddedNdb9uYD9qAjkzN0inBhvQ-mp6uJLgguEhv_TeE,2462
8
- ltq/task.py,sha256=anX9jKGGrNr8JDEhJ4L-QALbOUSDWkYHRwjyZDYCCDs,1008
9
- ltq/worker.py,sha256=dMYiX4oz_PCq0yytI0WalPPamBdVNXg1QVH92lRZkX0,2866
10
- ltq-0.1.0.dist-info/WHEEL,sha256=XV0cjMrO7zXhVAIyyc8aFf1VjZ33Fen4IiJk5zFlC3g,80
11
- ltq-0.1.0.dist-info/entry_points.txt,sha256=OogYaOJ_RORrWtrLlEL_gTN9Vx5tkgawl8BO7G9FKcg,38
12
- ltq-0.1.0.dist-info/METADATA,sha256=rfFU1puSrxGuiwDHTVT7ofUfMtwSx7FvawqnXo7YgqQ,2303
13
- ltq-0.1.0.dist-info/RECORD,,
8
+ ltq/task.py,sha256=JOeyulGJ-jSM_wzYyKPTPElRGx7Ncu6n1dRdn9NGwMI,1184
9
+ ltq/worker.py,sha256=PAMW8hvTuWCUALw41TnVBmcCxXytJbgVsxALAY9qmFY,3031
10
+ ltq-0.1.2.dist-info/WHEEL,sha256=XV0cjMrO7zXhVAIyyc8aFf1VjZ33Fen4IiJk5zFlC3g,80
11
+ ltq-0.1.2.dist-info/entry_points.txt,sha256=OogYaOJ_RORrWtrLlEL_gTN9Vx5tkgawl8BO7G9FKcg,38
12
+ ltq-0.1.2.dist-info/METADATA,sha256=7Mvz05MwsRJ5ddbegQofl9iLDHgELxwY2JEsSnJGK5Q,2316
13
+ ltq-0.1.2.dist-info/RECORD,,
File without changes