ltq 0.1.1__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.1
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>
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "ltq"
3
- version = "0.1.1"
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,7 @@ 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
7
 
8
8
  import redis.asyncio as redis
9
9
 
@@ -21,6 +21,7 @@ logger = get_logger()
21
21
 
22
22
 
23
23
  P = ParamSpec("P")
24
+ R = TypeVar("R")
24
25
 
25
26
 
26
27
  class Worker:
@@ -41,8 +42,8 @@ class Worker:
41
42
  self,
42
43
  queue_name: str | None = None,
43
44
  ttl: int | None = None,
44
- ) -> Callable[[Callable[P, Awaitable[Any]]], Task[P]]:
45
- 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]:
46
47
  filename = Path(fn.__code__.co_filename).stem
47
48
  task_name = f"{filename}:{fn.__qualname__}"
48
49
  queue = Queue(self.client, queue_name or task_name)
@@ -50,7 +51,7 @@ class Worker:
50
51
  name=task_name,
51
52
  fn=fn,
52
53
  queue=queue,
53
- ttl=ttl,
54
+ ttl=ttl,
54
55
  )
55
56
  self.tasks.append(task)
56
57
  return task
@@ -89,5 +90,8 @@ class Worker:
89
90
  await task.queue.ack(messages)
90
91
 
91
92
  async def run(self) -> None:
92
- workers = (self.worker(task) for task in self.tasks)
93
- 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
File without changes