Reputation: 1652
Please consider the following code:
from multiprocessing import Process
import time
myArray = []
def myThread():
counter = 0
global myArray
while True:
myArray.append(counter)
time.sleep(1)
counterThread = Process(target=myThread,)
counterThread.start()
while True:
if len(myArray) > 0:
print "Success"
else:
print ":("
print myArray
time.sleep(1)
I am unable to get my success message, and i'm not sure why, I keep receiving :(
and my terminal printing an empty array. I thought making the array global would mean any changes made at myThread()
level would be applied?
Upvotes: 1
Views: 566
Reputation: 459
You are creating an second process, which has no access to data of the main process. You can use threading.Thread(target=myThread,), but you has to synchronize the access threading.Lock(), if you are using more than one thread.
You should terminate your thread, when you are finished and wait for the thread with athread.join().
See: https://docs.python.org/2/library/threading.html
Upvotes: 2