chihuo: An Asynchronous Task Running Engine
Project description
chihuo: An Asynchronous Task Running Engine
chihuo is an asynchronous task running engine. It runs asynchronous tasks concurrently from a queue which can be at memory, Redis, Lodis or Some MQ.
The Loop
ChihuoLoop is the asynchronous engine core which responses to connect the backend queue
and distributes every tasks to the global event loop.
For a process, the chihuo create only one global event loop to run all asynchronous tasks and the loop is running forever.
-
ChihuoLoop.startmethodWhen a
ChihuoLoopstarts, it needs to add some primary tasks to the backend queue. Thestartfunction is the place to fire these priorities. -
ChihuoLoop.add_taskmethodAt anywhere and anytime, we can use the
add_taskmethod to add a task to backend queue. The task discription must be the tuple as(task_id: str, task_info: jsonifiable object_)e.g.add_task(("task1", [1, 1+10]), ("task2", [2, 2+10])) -
ChihuoLoop.make_taskmethodEach classes which have implemented the
ChihuoLoopclass must to implement themake_taskmethod for receiving the task's information that is added byChihuoLoop.add_taskto the backend queue. -
ChihuoLoop.task_finishmethodWhen a task is finished and is unneeded at future, we can tag the task as the
finishedstate using thetask_finishmethod. e.g.task_finish(task_id). The task that is atfinishedstate can't be add to backend queue again. -
ChihuoLoop.task_unfinishmethodWhen a task is at the
finishedstate and we need to add it to queue again, we can use thetask_unfinishmethod to set the task state tounfinish. Then, we can useChihuoLoop.add_taskto add the task to queue again. -
ChihuoLoop.stoppropertyWhen the property
stopto be set as True, theChihuoLoopwill stop pull the tasks from queue and exit.
Demo
# filename: abc.py
from chihuo import ChihuoLoop
from chihuo.common import SERVER_DEFAULT_CONFIG_PATH
import chihuo
class MyTasksA(ChihuoLoop):
NAME = "my-tasksA" # The unique id for backend queue server
CONCURRENCY = 10 # The number of tasks running concurrently
SERVER_CONFIG = SERVER_DEFAULT_CONFIG_PATH
def __init__(self):
super().__init__()
async def start(self):
for id in range(10):
await self.add_task((str(i), {i ** 2}))
async def make_task(self, task_id, task):
print(f"my-task-A: {task_id}")
print(f"power of {i} is {task}")
# Talk chihuo that the task is finished
await self.task_finish(i)
class MyTasksB(ChihuoLoop):
NAME = "my-tasksB" # The unique id for the loop
CONCURRENCY = 10 # The number of tasks running concurrently
SERVER_CONFIG = SERVER_DEFAULT_CONFIG_PATH
def __init__(self):
super().__init__()
async def start(self):
for id in range(10):
await self.add_task((str(i), {i ** 2}))
async def make_task(self, task_id, task):
print(f"my-task-B: {task_id}")
print(f"power of {i} is {task}")
# Talk chihuo that the task is finished
await self.task_finish(i)
if __name__ == '__main__':
chihuo.run(MyTasksA, MyTasksB)
To Run the project, firstly we need to launch a backend queue server. Here, we use a Lodis instance as default server.
Start a Lodis server:
LODIS_DB_PATH=/data/test-lodis LODIS_IP_PORT="127.0.0.1:8311" nohup /path/to/lodis &
Then, we write a configure file as ./server.json (the SERVER_DEFAULT_CONFIG_PATH)
to connect the backend for our MyTasksA and MyTasksB.
backend=lodis
ip=127.0.0.1
port=8311
Now, we can run the script.
python3 abc.py
Shutdown the Tasks
Defaultly, the global loop is running forever and we need to shutdown the loop by sending
a INT (interrupt) signal to the process. After the process received The INT signal, it will
set the property stop of instance which is implemented from ChihuoLoop to True and the
instance will stop all running tasks and send them back to queue, finally exit.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file chihuo-0.4.0.tar.gz.
File metadata
- Download URL: chihuo-0.4.0.tar.gz
- Upload date:
- Size: 11.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.3.2 CPython/3.9.9 Darwin/22.3.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
43a51fa5fc7a864498d6cc9871f4f7a476991559e304162bfcdf069865a03605
|
|
| MD5 |
adb725c28f587000550306159b495fc2
|
|
| BLAKE2b-256 |
0c7f95b9c24c3cd356517050c3e0166f5e839f7247438cceb1682194a9ff7f83
|