0

I'm started a concurrent program using Python. I have a simple question: I'm using the class Thread to create several concurrent processes. I would like these threads to pass objects to each other (so I would like that a thread not only notice a certain event, but takes an object from this event (when it happens)!). How can I do this? Thanks a lot!

Example of code:

class Process(Thread):
def __init__(self):
    super(Processo, self).__init__()

def run(self):
    myObject = SomeObject(param1, param2, param3)
    # Code to pass myObject to all the threads

    # Code waiting for the object to use it

def main():
   process1 = Process()
   process2 = Process()
   process3 = Process()

   process1.start()
   process2.start()
   process3.start()
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
claudioz
  • 1,121
  • 4
  • 14
  • 25
  • Please provide some code and then I might be able to help you with this. –  Mar 08 '19 at 13:18
  • Code added to the answer.... – claudioz Mar 08 '19 at 13:24
  • Add `global myObject` and all your `Thread`'s can access this object. You have to use [lock-objects](https://docs.python.org/3/library/threading.html#lock-objects) if concurent `read/write` could be the case. BTW, a `Thread` **is not** a `Process`, read [Multiprocessing vs Threading Python](https://stackoverflow.com/questions/3044580/multiprocessing-vs-threading-python) – stovfl Mar 08 '19 at 14:57
  • Use `queue.Queue` [Queue docs](https://docs.python.org/3/library/queue.html) – alberand Mar 11 '19 at 10:30

1 Answers1

0

You can use queue.Queue to communicate between threads. There is simple example of two threads. One thread puts items on the one side of the queue and the other take them. The queue can be also used for multi-producer multi-consumer application (but before implementing plan how it should work).

from queue import Queue
from random import randint
import threading 
import time

class Producer(threading.Thread):
    def __init__(self, queue):
        super(Producer, self).__init__()
        self.queue = queue

    def run(self):
        while True:
            thread_name = self.name

            if not self.queue.full():
                print('Will send "{}"'.format(thread_name))
                self.queue.put(thread_name)

            time.sleep(randint(1, 3))

class Consumer(threading.Thread):
    def __init__(self, queue):
        super(Consumer, self).__init__()
        self.queue = queue

    def run(self):
        while True:
            if not self.queue.empty():
                received = self.queue.get()
                print('Received "{}"'.format(received))

            time.sleep(randint(1, 3))

if __name__ == '__main__':
    q = Queue()

    a = Producer(queue=q)
    b = Consumer(queue=q)

    a.start()
    b.start()

    a.join()
    b.join()

The sleeps are only to slow it down for demonstration. There is documentation for Queue

Note: Thread is not the same as a Process. Be careful with terminology you can be miss-understood.

alberand
  • 632
  • 5
  • 13