I am having a hard time getting my code to properly run in parallel. What I need is the following:
- I have 4 disk codes that I need to evolve for a certain amount of time, in discrete time steps
- I want to evolve each of those codes in parallel
Because of the nature of the codes and of the data that I need to store, after trying processes pools, I've decided my best way to go is to use a Queue. I have tried two different codes. For this example, disk_codes are just numbers, but in my actual code they are individual instances of another code to solve equations of motion in a disk.
Code 1:
import multiprocessing
try:
import queue
except:
import Queue as queue
def evolve_single_disk(queue, dt):
print "Empty queue? ", queue.empty()
code = queue.get()
print "Evolving disk {0} in {1}".format(code, multiprocessing.current_process().name)
queue.task_done()
if __name__ == '__main__':
ncores = 4
code_queue = queue.Queue()
disk_codes = range(ncores)
for disk in disk_codes:
code_queue.put(disk)
dt = 1
t_end = 10
t = 0
# Evolve!
while t < t_end:
print "t=", t
processes = multiprocessing.Process(target=evolve_single_disk, args=(code_queue, dt, ))
processes.start()
processes.join()
disk_codes = code_queue.get()
print "disk codes: ", disk_codes
t += dt
This results in:
t= 0
Empty queue? False
Evolving disk 0 in Process-1
disk codes: 0
t= 1
Empty queue? False
Evolving disk 1 in Process-2
disk codes: 1
t= 2
Empty queue? False
Evolving disk 2 in Process-3
disk codes: 2
t= 3
Empty queue? False
Evolving disk 3 in Process-4
disk codes: 3
t= 4
Empty queue? True
So, in every time step, one of the disks is "evolved". This is not what I want, since I want all four disks to be evolved in parallel in the same time step.
Then I tried this:
Code 2:
import multiprocessing
try:
import queue
except:
import Queue as queue
def evolve_single_disk(queue, dt):
print "Empty queue? ", queue.empty()
code = queue.get()
print "Evolving disk {0} in {1}".format(code, multiprocessing.current_process().name)
queue.task_done()
if __name__ == '__main__':
ncores = 4
code_queue = queue.Queue()
disk_codes = range(ncores)
for disk in disk_codes:
code_queue.put(disk)
dt = 1
t_end = 10
t = 0
# Evolve!
while t < t_end:
print ""
print "t=", t
processes = [multiprocessing.Process(target=evolve_single_disk, args=(code_queue, dt, )) for x in range(ncores)]
for p in processes:
p.start()
p.join()
disk_codes = [code_queue.get() for p in processes]
print "disk codes: ", disk_codes
t += dt
Which results in:
t= 0
Empty queue? False
Evolving disk 0 in Process-1
Empty queue? False
Evolving disk 0 in Process-2
Empty queue? False
Evolving disk 0 in Process-3
Empty queue? False
Evolving disk 0 in Process-4
disk codes: [0, 1, 2, 3]
t= 1
Empty queue? True
...and then the code just hangs. So here I am starting 4 processes in every time step, but each process is receiving the exact same disk.
How can I do this properly, so that on each time step there are 4 processes and each one of them evolves 1 single disk? I have read the documentation and many tutorials and SO questions/answers, but I am still very confused.
Edit:
I tried using multiprocessing queues, but then I get a TypeError when I try to put disk codes into the queue. Sadly the disk codes are also not pickleable. Traceback when using multiprocessing queue with disk codes:
t= 0
<multiprocessing.queues.Queue object at 0x7fee4a2d3950>
Traceback (most recent call last):
File "/home/fran/anaconda2/lib/python2.7/multiprocessing/queues.py", line 268, in _feed
send(obj)
TypeError: expected string or Unicode object, NoneType found
Traceback (most recent call last):
File "/home/fran/anaconda2/lib/python2.7/multiprocessing/queues.py", line 268, in _feed
send(obj)
TypeError: expected string or Unicode object, NoneType found
Traceback (most recent call last):
File "/home/fran/anaconda2/lib/python2.7/multiprocessing/queues.py", line 268, in _feed
send(obj)
TypeError: expected string or Unicode object, NoneType found
Traceback (most recent call last):
File "/home/fran/anaconda2/lib/python2.7/multiprocessing/queues.py", line 268, in _feed
send(obj)
TypeError: expected string or Unicode object, NoneType found
Empty queue? True