Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recommended way to send messages between threads in python?

I have read lots about python threading and the various means to 'talk' across thread boundaries. My case seems a little different, so I would like to get advice on the best option:

Instead of having many identical worker threads waiting for items in a shared queue, I have a handful of mostly autonomous, non-daemonic threads with unique identifiers going about their business. These threads do not block and normally do not care about each other. They sleep most of the time and wake up periodically. Occasionally, based on certain conditions, one thread needs to 'tell' another thread to do something specific - an action -, meaningful to the receiving thread. There are many different combinations of actions and recipients, so using Events for every combination seems unwieldly. The queue object seems to be the recommended way to achieve this. However, if I have a shared queue and post an item on the queue having just one recipient thread, then every other thread needs monitor the queue, pull every item, check if it is addressed to it, and put it back in the queue if it was addressed to another thread. That seems a lot of getting and putting items from the queue for nothing. Alternatively, I could employ a 'router' thread: one shared-by-all queue plus one queue for every 'normal' thread, shared with the router thread. Normal threads only ever put items in the shared queue, the router pulls every item, inspects it and puts it on the addressee's queue. Still, a lot of putting and getting items from queues....

Are there any other ways to achieve what I need to do ? It seems a pub-sub class is the right approach, but there is no such thread-safe module in standard python, at least to my knowledge.

Many thanks for your suggestions.

like image 502
Blindfreddy Avatar asked Jun 09 '26 12:06

Blindfreddy


1 Answers

Instead of having many identical worker threads waiting for items in a shared queue

I think this is the right approach to do this. Just remove identical and shared from the above statement. i.e.

having many worker threads waiting for items in queues

So I would suggest using Celery for this approach.

Occasionally, based on certain conditions, one thread needs to 'tell' another thread to do something specific - an action, meaningful to the receiving thread.

This can be done by calling another celery task from within the calling task. All the tasks can have separate queues.

like image 111
Muhammad Tahir Avatar answered Jun 12 '26 03:06

Muhammad Tahir