2

This is my code:

from multiprocessing import Process, Manager, Value
from ctypes import c_char_p

def greet(string):
    print('bob')
    string.value = '1'
    for i in range(100):
        string.value = str(i)

if __name__ == '__main__':
    manager = Manager()
    string = manager.Value(c_char_p, "Hello")
    process = Process(target=greet, args=(string,))
    process.start()
    for j in range(100):
        print(string.value)
    process.join()
    input()

Now I expect the code to print something like:

Hello
1
1
1
4
4
5
5
6
7

Because, of course, I understand that both the loops will probably be running and different speeds. But all the code prints is a Hello, a hundred times, it doesn't even print bob until the code ends and I call process.join(). It is like the greet doesn't run until I call process.join(). And I have read Python multiprocessing not calling function, and I am running the code from command line. It still doesn't work

First of all, I'd like it if anyone could tell me why the function is being called only in the end, and how to fix it. If that can be fixed, will string still be readable by both the parent and the child process?

Thanks in advance!

1 Answers1

1

Your code is fine. I think this really is just a timing problem on your specific machine. When I ran your code, I could see ca. 70x "hello" and then numbers. So I started inserting sleep()s and it's really interesting to see what happens:

from multiprocessing import Process, Manager, Value
from ctypes import c_char_p                                                 
import time                                                                 

def greet(string):                                                          
    print('bob')                                                            
    string.value = '1'                                                      
    for i in range(100):                                                    
        string.value = str(i)                                               
        time.sleep(0.1)

if __name__ == '__main__':                                                  
    manager = Manager()                                                     
    string = manager.Value(c_char_p, "Hello")                               
    process = Process(target=greet, args=(string,))                         
    process.start()                                                         
    time.sleep(0.5)
    for j in range(100):                                                    
        print(string.value)                                                 
        time.sleep(0.2)
    process.join()                                                          
    input()                                                                 

On my machine, I now get:

bob
4
6
8
10
...

No "hello" because the sleep(0.5) in __main__ is virtually guaranteed to be slower than the newly forked "greet" process, so when finally the first print(string.value) is executed, the "hello" has long been overwritten.

Your code's fine, but your assumptions as to which part of the program takes how long are wrong (at least for your machine). Play with the sleeps a bit. It's very instructive.

digitalarbeiter
  • 2,295
  • 14
  • 16