没有创建多个进程的Python [英] Multiple process is not getting created Python

查看:89
本文介绍了没有创建多个进程的Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做的任务是创建多个进程以并行运行代码以加快进程. 下面是我的代码.

I am working on task where I am creating multiple process to run code in parallel to speed up process. below is my code.

def update_value(value):
    print('module name:\n', __name__)
    print('parent process:\n', os.getppid())
    print('process id:\n', os.getpid())
    value_read = server_connect_read(channel, value)
    if value_read.server_connect() is False:
        return False
    print("updating values")
    update = server_read.update_value(old_values.xlsx)
    if value_read.server_disconnet() is False:
        return False

Pool(3, initializer=print('starting', current_process().name )).map(update_value, (ValueList,))

在上面的代码中,ValuList是excel文件,其中包含需要更新的值.现在,当我在上面的代码上运行时,我将得到下面的输出.

In Above code, ValuList is excel file containing values that needed to update. Now when I run above code I am getting below as output.

module name:
 __main__
parent process:
 8048            <-----
process id:
 15068           <-----
module name:
 __main__
parent process:

8048          <-----
process id:
 15068      <----

在此过程中,第一个代码将从本地文件读取值,建立连接,从服务器读取值,更新到本地文件.

In the process, first code will reads value from local file, establish connection, reads value from server, updates to local file.

上面的代码运行,我可以看到正在创建进程.但是所有进程的父ID和进程ID保持不变. 根据我的理解,每个进程都会有自己的进程ID.

Above code runs and I can see process is getting created. But all process parent id and process id remains same. As per my understanding, each process will have their own process id's.

我需要帮助确定代码中是否有错误.

I need help in figuring out if any mistakes in the code.

推荐答案

在对该主题进行了更多搜索之后,我发现该进程执行fork(),然后执行一个全新的Python进程的execve().解决了问题,因为模块状态从头开始. 在下面的代码中.

After doing some more searches over the topic I found that the process does a fork() followed by an execve() of a completely new Python process. That solved problem, because module state starts from scratch. In code as below.

if __name__ == '__main__':
    with get_context("spawn").Pool(2) as pool:
        pool.map(update_value, ValueList)

希望这会有所帮助

这篇关于没有创建多个进程的Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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