Parallel multiprocessing in python easy example

久未见 提交于 2019-12-12 09:24:07

问题


I need to say that multiprocessing is something new to me. I read some about it but it makes me more confused. I want to understand it on a simple example. Let's assume that we have 2 functions in first one I just increment 'a' variable and then assign it to 'number' variable, in second I start first function and each every one second I want to print 'number' variable. It should looks like:

global number

def what_number():
    a=1
    while True:
       a+=1
       number=a

def read_number():
    while True:
       --> #here I need to start 'what_number' function <--
        time.sleep(1)
        print(number)


if __name__ == "__main__":
    read_number()

How can I do that? Is there an easy and proper way to do that ?

UPDATE:

I saw noxdafox answer I'm really thankfull but it isn't exactly what I want. First of all I don't want send value in first function ('main' in noxdafox code). Second I don't want to get all values so quene will won't work. I need to get after each second number of while loops. Code should be something like :

import multiprocessing
import time

number = 0


def child_process():
    global number
    while True:
        number += 1
        print(number)


def main():
    process = multiprocessing.Process(target=child_process)
    process.start()

    while True:
       print("should get same number:",number)
       time.sleep(0.001)

if __name__ == "__main__":
    main()

If u run above code you get something like:

but this blue selected values should be same ! and that's the main problem :)

P.S sorry for chaos


回答1:


Ok it takes some time but I figured it out. All it was about Sharing state between processes now all it works like charm. Code :

from multiprocessing import Process, Value
import time


def child_process(number):
    number.value = 0
    while True:
        number.value += 1
        #print(number)


def main():
    num = Value('i')
    process = Process(target=child_process, args=(num,))
    process.start()
    while True:
       print("should get same number:", num.value)
       time.sleep(1)

if __name__ == "__main__":
    main()



回答2:


As processes live in separate memory address spaces, you cannot share variables. Moreover, you are using global variables incorrectly. Here you can see an example on how to use global variables.

The most straightforward way to share information between processes is via a Pipe or Queue.

import multiprocessing

def child_process(queue):
    while True:
        number = queue.get()
        number += 1
        queue.put(number)

def main():
    number = 0
    queue = multiprocessing.Queue()
    process = multiprocessing.Process(target=child_process, args=[queue])
    process.start()

    while True:
        print("Sending %d" % number)
        queue.put(number)

        number = queue.get()
        print("Received %d" % number)

        time.sleep(1)

if __name__ == "__main__":
    main()


来源:https://stackoverflow.com/questions/47428768/parallel-multiprocessing-in-python-easy-example

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!