python threading multiprocess_python multiprocess无法启动

Here is my code for a simple multiprocessing task in python

from multiprocessing import Process

def myfunc(num):

tmp = num * num

print 'squared O/P will be ', tmp

return(tmp)

a = [ i**3 for i in range(5)] ## just defining a list

task = [Process(target = myfunc, args = (i,)) for i in a] ## creating processes

for each in task : each.start() # starting processes

for each in task : each.join() # waiting all to finish up

When I run this code, it hangs at certain point, so to identify it I ran it line by line in python shell and found that when I call 'each.start()' The shell pops out a dialogue box as:

" The program is still running , do you want to kill it? '

and I select 'yes' the shell closes.

When I replace Process with 'threading.Thread' the same code runs but with this nonsense output:

Squared Squared Squared Squared Squared 0 1491625

36496481

Is there any help in this regard ? thank in advance

To run my python codes I use Idlex IDE and I start it from terminal.

I have Intel Xeon Processor with 4 cores / 8 Threads, and 8GB RAM

解决方案

With a little thought I finally found the problem.

This is happening because in Python, the float and int objects are not 'thread-safe', meaning the memory allocated to calculate any function's value by one thread/process can be overwritten by another and hence they show absurd values. This is called a race condition.

To solve this problem, use deque() from the collections module or, even better, use the 'Lock' facility. deque() works with arrays but it's meant for arrays of the same kind (much like MATLAB arrays) and is thread/process safe. 'Lock' avoids race conditions.

So the edit would be :

def myfunc(num):

lock.acquire()

.......some code .....

.......some code......

lock.release()

That's all.

But one problem still persists and that is with the multiprocessing module. Even after calling 'lock', the problem mentioned in the question remains.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值