python multiprocess无法启动 [英] python multiprocess fails to start

查看:672
本文介绍了python multiprocess无法启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在python中进行简单多处理任务的代码

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 <------ problem line

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

当我运行这段代码时,它会挂在某个位置,因此为了识别它,我在python shell中逐行运行它,发现当我调用"each.start()"时,shell弹出对话框,如下所示:

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.

当我将Process替换为'threading.Thread'时,运行相同的代码,但输出如下:

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

要运行python代码,请使用Idlex IDE,然后从终端启动它.

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

我有带4核/8线程和8GB RAM的Intel Xeon处理器

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

推荐答案

稍加思考,我终于找到了问题.

With a little thought I finally found the problem.

之所以发生这种情况,是因为在Python中,float和int对象不是线程安全的",这意味着分配给一个线程/进程计算任何函数值的内存可以被另一个线程/进程覆盖,因此它们显示出荒谬的值.这称为竞赛条件.

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.

要解决此问题,请使用collections模块中的deque(),或者甚至更好地使用"Lock"功能. deque()可用于数组,但它适用于相同类型的数组(非常类似于MATLAB数组),并且是线程/进程安全的. 锁定"避免了比赛条件.

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.

因此,修改应为:

def myfunc(num):

    lock.acquire()

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

    lock.release()

仅此而已.

但是一个问题仍然存在,那就是多处理模块.即使调用了锁定",问题中提到的问题仍然存在.

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

这篇关于python multiprocess无法启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆