Deadlock problem using multiprocessing

蓝色基因 bluegene8210 at gmail.com
Sun Sep 11 05:54:01 CEST 2011


This is my first touch on the multiprocessing module, and I admit not
having a deep understanding of parallel programming, forgive me if
there's any obvious error. This is my test code:

# deadlock.py

import multiprocessing

class MPTask:
	def __init__(self):
		self._tseq= range(10)	# task sequence
		self._pool= multiprocessing.Pool(2)	# process pool
	def _exe(self, num):
		return num**2
	def run(self):
		result= self._pool.map_async(self._exe, self._tseq)
		return result.get()

result= MPTask().run()
print(result)

And seemingly it creates a deadlock, I have to manually kill the
processes in the system monitor, yet it would be OK if the _exe()
function was defined outside the MPTask class:

# no_deadlock.py
import multiprocessing

def _exe(num):
	return num**2

class MPTask:
	def __init__(self):
		self._tseq= range(10)	# task sequence
		self._pool= multiprocessing.Pool(2)	# process pool
	def run(self):
		result= self._pool.map_async(_exe, self._tseq)
		return result.get()

result= MPTask().run()
print(result)

This would give the correct answer without any deadlock. My questions:

1. Why is this, where did the deadlock come from?

2. Besides, is there any material I can refer to about how to avoid
deadlocks when using multiple processes in the program?

Thanks!




More information about the Python-list mailing list