Home
mq is a publish/subscribe (or queueing system) mechanism based on mongodb
. It allows to launch small tasks
in an asynchronous way. Several tools exist but using other datastore such as Redis (rq, ...) so if you already use
redis, it may be a better choice.
mq is a hobby project mainly to learn asyncio module, and multiprocessing condition primitives.
Getting started 🚀
How it works ⁉
mq can work in several ways. Usually, we enqueue tasks in a process, and launch another process to dequeue these tasks and perform their execution. mq support this in several ways:
- launch worker process in same script becoming a subprocess of the main process
- launch worker process in another script.
- launch worker in a thread for heavy IO tasks.
Examples 🎨
You can also simply wait for the job result
Or cancel the job while it is running:
Launch worker process in another script
Needs to add a parameter in the init function of mq, in the enqueuing process
await mq.with_process_connection(
# specify connection parameters to the main manager process
MQManagerConnectionParameters(
url= "127.0.0.1",
port=50000,
authkey=b"abracadabra"
)
).init(
# specify mongodb connection
MongoDBConnectionParameters(
mongo_uri="mongodb://localhost:27017",
db_name="mq",
collection="mq",
),
# add this one to start manager server
start_server=True,
)
Now in another script:
await mq.with_process_connection(
# specify connection parameters to the main manager process
MQManagerConnectionParameters(
url= "127.0.0.1",
port=50000,
authkey=b"abracadabra"
) #(1)
).init(
# specify mongodb connection
MongoDBConnectionParameters(
mongo_uri="mongodb://localhost:27017",
db_name="mq",
collection="mq",
),
in_worker_process=True,
)
- Needs to connect to other process and acceed to shared memory. Process can also be remote ☁ !