I have a Python script using the multiprocessing and sacn library:
from multiprocessing import Process
import time
import sacn
def GettingDMXValues():
while True:
receiver = sacn.sACNreceiver(bind_address = "***.**.**.**")
receiver.start()
@receiver.listen_on('universe', universe=1)
def callback(packet):
#print(packet.dmxData)
global DMXValues
DMXValues = packet.dmxData
print(DMXValues)
receiver.join_multicast(1)
time.sleep(10)
receiver.leave_multicast(1)
receiver.stop()
def printtest2():
while True:
print(DMXValues)
p1 = Process(target=GettingDMXValues)
p2 = Process(target=printtest2)
if __name__ == '__main__':
p1.start()
time.sleep(1)
p2.start()
For the first function, everything works fine, but when the second function tries to print the DMXValues it says they are not defined, even though they have been printed in the other function.
I also can't access the Variable from anywhere else than the callback function
When debugging, depending on where the breakpoint is set, the Variable is either undefined or the Values I am expecting.
If I set the Variable at the start the second function prints the at the start defined Value, while the second one prints the Values I am expecting.
I searched but I did not find anyone with the same problem.
Aaron's comment about processes not being able to share global variables is your initial problem, but not you only problem. You have:
But even assuming you could share variables, (1)
DMXValueswill not be defined until functioncallbackassigns a value to this variable and you will most likely get an exception if waiting 1 second to start theprinttest2process is enough time. And even assumingcallbackcreates the variable beforeprinttest2runs and you get no exception, you will be in an endless loop printing potentially the same value over and over again until a new value is assigned incallback.Assuming you want to print the value of
DMXValuesonce each time a new value is assigned, then you should be using amultiprocessing.Queueto which one process will be putting new values and the other process will be getting them for printing: