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!