3

I am looking for a way to pass values(ex integers,arrays) between multiple threads in Python. I understand that this task can be achieved by using the Queue module, but I am not very familiar neither with python or this specific module.

I have the following scenario: each thread needs to do some calculations based on its own data or data from other threads. Also each thread knows what other thread holds the data it needs for a specific job (all threads have an array of all threads, so any thread knows that for a task X he needs to get the data from a specific thread(row,col) from that array).

How can this communication between threads be done using the Queue module or perhaps another technique(the Queue module seemed to be the right thing for this job). Any help is most appreciated. Thanks a lot.

1 Answer 1

3

Using queues

Usually, a queue is used in a scenario with a bunch of worker threads that get their jobs from the queue. Free threads are waiting on the queue for new jobs to be put in it. Then the job is executed by a thread while all remaining threads are waiting for the next job. If there are more jobs posted than threads are available the queue starts to fill up.

That doesn't apply to your scenario as you describe it. Maybe you can just read the data directly without putting it in a queue. If you write in shared data structures, you may consider a locking strategy.

You should read up on parallel programming in general. The concepts are fairly language independent. Then you can read a tutorial about threads with Python. There is plenty of material on the internet about both topics.

Edit:

Communication between threads using threading.Event

The simplest way to communicate between two threads is a threading.Event . The event can be set to true or false. Usually, one thread is setting the event and another thread checks the value of the Event and acts accordingly. For example, the event could indicate that there is something new to do. The indicating thread first fills up data structures that are necessary for the upcoming task and then sets the event true. Another thread that was waiting on the event is activated after the event is true. Subsequently, it reads out the data structures and performs the task.

Sign up to request clarification or add additional context in comments.

4 Comments

Can you think of any idea of how a thread can inform another specific thread that it requires something? A way of communication between a thread and another specific thread? Thank you
There are many ways to achieve that (you can even (mis-)use queues for that). I would go for a simple threading.Event and only make it more complex if absolutely necessary.
Would it be possible to do something like: the asking thread initializes different events for different threads from which he wants data(if he wants data from thread(row,col) he creates an event<row><col>). And each thread in its run method handles event<row><col>. Each thread can have a row, col attributes that can be set in the init method of each thread. I am not very coherent, sorry
@biggdman That sounds technically possible. But maybe you want to consult another programmer in person before you implement your ideas. It's too hard to give correct advice here given the limited information about your project.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.