简体   繁体   English

Python 3中模块线程和多处理的使用是否互斥?

[英]Are the use of module threading and multiprocessing mutually exclusive in Python 3?

Using Python 3.4 on Linux 在Linux上使用Python 3.4

I'm new to parallel programming and I'm encountering problems when running the threading.Threads() for a specific method, and the module multiprocessing.Process() for another. 我是并行编程的新手,为特定方法运行threading.Threads(),为另一个方法运行模块multiprocessing.Process()时遇到问题。 Both methods work fine when the other one is commented out. 当另一种方法被注释掉时,两种方法都可以正常工作。 Neither method has anything to do with the other (eg no attempt to share data). 两种方法都与另一种方法无关(例如,不尝试共享数据)。 But when I have them both running neither works and everything freezes. 但是当我让它们都无法运行时,一切都冻结了。 As far as I can tell the multiprocessing seems to lock up. 据我所知,多处理似乎已锁定。 I assume the same thing applies for the Threading. 我认为同样的事情适用于线程。

So the first step is to assert whether or not this is even possible? 因此,第一步就是断言这是否可能?

(I have a feeling some of you will ask the reason for this... The threading does a simple capture user key checking while the multiprocessing does some heavy lifting) (我感觉你们中的一些人会问这是为什么...线程在多处理过程中进行了一些繁重的工作,而线程则进行了简单的捕获用户密钥检查)

I'm providing an example (more like pseudo code) to help illustrate how the methods are used. 我提供一个示例(更像伪代码)来帮助说明如何使用这些方法。

file t.py 文件t.py

import threading

Class T:

    Def __init__():
        t = threading.Thread(target = self.ThreadMethod)
        t.daemon = True
        t.start()

    Def ThreadMehod():
        # capture key

file m.py 文件m.py

import multiproceessing

Class M:

    Def __init__():
        mp = multiprocessing.Process(target = self.ProcessMethod)
        mp.start()

    Def ProcessMethod():
        # heavy lifting

file main.py 文件main.py

import T
import M

Class main:

  Def __init__():
     T()

  Def DoTheProcess()
     for i in range(5):
       M()

"no. threading and multiprocessing are not mutually exlusive. Though there are known issues (eg, the reason for atfork existance) that constrain how they can be used together." “没有。线程和多处理不能相互排斥。尽管存在已知的问题(例如,存在atfork的原因)限制了它们如何一起使用。” - JF Sebastian -塞巴斯蒂安(JF Sebastian)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM