2

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: enter image description here

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

P.S sorry for chaos

KyluAce
  • 654
  • 6
  • 15

2 Answers2

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()
KyluAce
  • 654
  • 6
  • 15
0

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()
noxdafox
  • 11,600
  • 3
  • 26
  • 37
  • Thanks for your answer but in this case I get number increased for 1 every 1sec . The point is to get number of while loops you know what I mean ? – KyluAce Nov 22 '17 at 08:09
  • I mean to get value of number where number is all the time increased for 1sec in while loop :) – KyluAce Nov 22 '17 at 08:11
  • there is a way to do it something like that ? https://pastebin.com/ehMv1j1p ? But for now it's giving all the time 0 – KyluAce Nov 22 '17 at 08:37