|
1 | 1 | # TaskIQ - PSQLPy
|
| 2 | + |
| 3 | +TaskIQ-PSQLPy is a plugin for taskiq that adds a new result backend based on PostgreSQL and [PSQLPy](https://github.com/qaspen-python/psqlpy). |
| 4 | + |
| 5 | +## Installation |
| 6 | +To use this project you must have installed core taskiq library: |
| 7 | +```bash |
| 8 | +pip install taskiq |
| 9 | +``` |
| 10 | + |
| 11 | +This project can be installed using pip: |
| 12 | +```bash |
| 13 | +pip install taskiq-psqlpy |
| 14 | +``` |
| 15 | + |
| 16 | +Or using poetry: |
| 17 | +``` |
| 18 | +poetry add taskiq-psqlpy |
| 19 | +``` |
| 20 | + |
| 21 | +## Usage |
| 22 | +Let's see the example with the redis broker and PostgreSQL PSQLPy result backend: |
| 23 | +```python |
| 24 | +# broker.py |
| 25 | +import asyncio |
| 26 | + |
| 27 | +from taskiq_redis import ListQueueBroker |
| 28 | +from taskiq_psqlpy import PSQLPyResultBackend |
| 29 | + |
| 30 | +psqlpy_result_backend = PSQLPyResultBackend( |
| 31 | + dsn="postgres://postgres:postgres@localhost:5432/postgres", |
| 32 | +) |
| 33 | + |
| 34 | +# Or you can use PubSubBroker if you need broadcasting |
| 35 | +broker = ListQueueBroker( |
| 36 | + url="redis://localhost:6379", |
| 37 | + result_backend=psqlpy_result_backend, |
| 38 | +) |
| 39 | + |
| 40 | + |
| 41 | +@broker.task |
| 42 | +async def best_task_ever() -> None: |
| 43 | + """Solve all problems in the world.""" |
| 44 | + await asyncio.sleep(5.5) |
| 45 | + print("All problems are solved!") |
| 46 | + |
| 47 | + |
| 48 | +async def main(): |
| 49 | + await broker.startup() |
| 50 | + task = await best_task_ever.kiq() |
| 51 | + print(await task.wait_result()) |
| 52 | + await broker.shutdown() |
| 53 | + |
| 54 | + |
| 55 | +if __name__ == "__main__": |
| 56 | + asyncio.run(main()) |
| 57 | +``` |
| 58 | + |
| 59 | +## PSQLPyResultBackend configuration |
| 60 | +- `dsn`: connection string to PostgreSQL. |
| 61 | +- `keep_results`: flag to not remove results from Redis after reading. |
| 62 | +- `table_name`: name of the table in PostgreSQL to store TaskIQ results. |
| 63 | +- `field_for_task_id`: type of a field for `task_id`, you may need it if you want to have length of task_id more than 255 symbols. |
| 64 | +- `**connect_kwargs`: additional connection parameters, you can read more about it in [PSQLPy](https://github.com/qaspen-python/psqlpy) repository. |
0 commit comments