ltq 0.1.0__tar.gz → 0.1.2__tar.gz

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.
@@ -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),
@@ -1,5 +1,5 @@
1
1
  <p align="center">
2
- <img src="assets/logo.png" alt="LTQ" width="400">
2
+ <img src="https://raw.githubusercontent.com/tclesius/ltq/refs/heads/main/assets/logo.png" alt="LTQ" width="400">
3
3
  </p>
4
4
 
5
5
  <p align="center">
@@ -18,11 +18,9 @@ uv add ltq
18
18
 
19
19
  ```python
20
20
  import asyncio
21
- import redis.asyncio as redis
22
21
  import ltq
23
22
 
24
- client = redis.from_url("redis://localhost:6379")
25
- worker = ltq.Worker(client=client)
23
+ worker = ltq.Worker(url="redis://localhost:6379")
26
24
 
27
25
  @worker.task()
28
26
  async def send_email(to: str, subject: str, body: str) -> None:
@@ -71,7 +69,7 @@ Add middleware to handle cross-cutting concerns:
71
69
  from ltq.middleware import Retry, RateLimit, Timeout
72
70
 
73
71
  worker = ltq.Worker(
74
- client=client,
72
+ url="redis://localhost:6379",
75
73
  middlewares=[
76
74
  Retry(max_retries=3, min_delay=1.0),
77
75
  RateLimit(requests_per_second=10),
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "ltq"
3
- version = "0.1.0"
3
+ version = "0.1.2"
4
4
  description = "Add your description here"
5
5
  readme = "README.md"
6
6
  authors = [{ name = "Tom Clesius", email = "tomclesius@gmail.com" }]
@@ -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)
@@ -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()
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes