在一个运行64位Python的64位多线程脚本中运行

2024-04-24 08:46:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要在32位系统范围内运行Python脚本,以便通过第三方应用程序生成/收集数据。但是我想通过numba使用GPU处理数据,所以它必须在64位Python环境中运行。在

我已经设置了一个64位Pythonvirtualenv,并测试了一些简单的numba代码,这些代码在其中运行良好。那么我该如何在父进程中编写代码来调用子进程(multiprocessing或{}),它将切换到64位virtualenv并使用numba进行计算?更具体地说:

  1. 我应该使用multiprocessing或{}来实现父进程(32位Python)和子进程(64位Python)机制吗?在
  2. 如何在父进程和子进程之间传递大量数据?在

可能的代码示例:

def func_32():
    # data collection
    # using 3rd party API
    return data

def func_64(data, output):
    # switch to 64 bit virtual env
    # using virtualenvwrapper-win
    os.system('workon env64bit')
    # numba data process
    # results stored in output
    return None

def main():
    data = func_32()
    # I think I only need one process since it will be in GPU not CPU
    p = multiprocessing.Process(target=func_64, args=(data, output))
    p.start()
    return output

我在示例代码中遗漏了什么?在


Tags: 数据代码in示例outputdatareturngpu
1条回答
网友
1楼 · 发布于 2024-04-24 08:46:37

我看到了这个问题Spawn multiprocessing.Process under different python executable with own path,并在给出Python版本(2.7.5 32位,2.7.15 64位)时发现了我的答案。在

def func_32():
    # data collection
    # using 3rd party API
    return data

def func_64(data, output):
    # switch to 64 bit Python
    # not directly calling virtualenv
    # to switch env
    import sys
    print sys.executable
    # will print  C:\Python64\Python.exe
    # numba data process
    # results stored in output
    return None

def main():
    data = func_32()
    multiprocessing.set_executable(r'C:\Python64\python.exe')
    p = multiprocessing.Process(target=func_64, args=(data, output))
    p.start()
    p.join()
    return output

但是为了在虚拟环境中使用64位Python包,我最终主要从activate_this.py(位于virtualenv文件夹中)复制代码,以更改Python搜索路径等。在

我认为使用multiprocessing可以更方便地在父进程和子进程之间传递数据,尤其是大量数据。在

相关问题 更多 >