2

here is a simple example:

from collections import deque
from multiprocessing import Process

global_dequeue = deque([])

def push():
    global_dequeue.append('message')

p = Process(target=push)
p.start()

def pull():
    print(global_dequeue)

pull()

the output is deque([])

if I was to call push function directly, not as a separate process, the output would be deque(['message'])

How can get the message into deque, but still run push function in a separate process?

2 Answers 2

1

You can share data by using multiprocessing Queue object which is designed to share data between processes:

from multiprocessing import Process, Queue
import time


def push(q):  # send Queue to function as argument
    for i in range(10):
        q.put(str(i))  # put element in Queue
        time.sleep(0.2)
    q.put("STOP")  # put poison pillow to stop taking elements from Queue in master


if __name__ == "__main__":
    q = Queue()  # create Queue instance
    p = Process(target=push, args=(q,),)  # create Process
    p.start()  # start it

    while True:
        x = q.get()
        if x == "STOP":
            break
        print(x)
    p.join()  # join process to our master process and continue master run
    print("Finish")

Let me know if it helped, feel free to ask questions.

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

2 Comments

Artiom Kozyrev that is it thanks. one question. if i'm putting in the queue list of jobs that don't finish sequentially, what should I do? call get, if the job id is not the one I'm looking for, put it back on the queue?
@ape_face yes, if you do not want to work with the task at the moment you can return it back to Queue, but you should use some synchronisation, instead of jutst posison pillow.
1

You can also use Managers to achieve this.

Python 2: https://docs.python.org/2/library/multiprocessing.html#managers

Python 3:https://docs.python.org/3.8/library/multiprocessing.html#managers

Example of usage:

https://pymotw.com/2/multiprocessing/communication.html#managing-shared-state

Comments

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.