I need to know how to write a program that runs two threads:
1. a thread the updates the position(values) of an object
2. a thread that runs two methods in it based on the values from the first thread
All of this in a loop
This is the current code that I have and I want to changed as I described, not that the value are form the valueList and the activate(valueList) is a method that contains several smaller methods in it. If you don’t mind i would love to have an example to solving this.Thanks
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect(('59.191.193.42',5555))
screenw = 0
screenh = 0
while 1:
client_socket.send("loc\n")
data = client_socket.recv(8192)
valueList = data.split()
if (not(valueList[-1] == "eom" and valueList[0] == "start")):
#print "continuing.."
continue
if(screenw != int(valueList[2])):
screenw = int(valueList[2])
screenh = int(valueList[3])
activate(valueList)
As others have pointed out, It doesn’t look like threading is actually going to solve your problem. Especially because Python code is subject to the Global Interpreter Lock, which basically means theading will only help you if your code is IO-bound (reading large files from disk, waiting on a slow network connection, etc). If your program is CPU-bound and you really want to take advantage of parallel processing, multiprocessing is the way to go. With multiprocessing, you trade some memory overhead and a little latency (when the process is created and during inter-process communication) for the ability to take advantage of multi-core CPUs.
Just on the off chance that parallel processing does turn out to be useful for your program, or you’re just curious, I present the following code sample. Disclaimer, I haven’t so much as tried to import this module, so consider it pseudocode.