- https://docs.python.org/3/library/threading.html
- https://docs.python.org/3/library/multiprocessing.html
- Python: what are the differences between the threading and multiprocessing modules?
在Linux上使用Python 3.4在Python 3中是否使用模块线程和多处理相互排斥?
我新的并行编程和运行threading.Threads当我遇到问题()为一个特定的方法,并为另一个模块multiprocessing.Process()。当另一个被注释掉时,这两种方法都可以正常工作。这两种方法都没有任何关系(例如,不尝试共享数据)。但是,当我把它们都运行的时候都不行,一切都冻结了。据我所知,多处理似乎锁定了。我认为同样的事情适用于线程。
所以第一步是断言这是否甚至可能?
(我有一种感觉有些人会问这样做的原因...的线程执行一个简单的捕捉用户密钥检查,同时多完成一些繁重)
我提供一个例子(更像是伪代码)来帮助说明如何使用这些方法。
文件t.py
import threading
Class T:
Def __init__():
t = threading.Thread(target = self.ThreadMethod)
t.daemon = True
t.start()
Def ThreadMehod():
# capture key
文件m.py
import multiproceessing
Class M:
Def __init__():
mp = multiprocessing.Process(target = self.ProcessMethod)
mp.start()
Def ProcessMethod():
# heavy lifting
文件main.py
import T
import M
Class main:
Def __init__():
T()
Def DoTheProcess()
for i in range(5):
M()
简短的回答是,我不认为这应该发生,我已经在同一个应用程序之前没有问题,虽然有一个不同的安排,但你检查,以确保你没有得到变量名称的重叠?你如何导入?你是从x导入*还是只导入x? –
我刚刚尝试使用池而不是过程,现在看来,这两个工作...就像我说的,我是新来的mp,所以我可能实施过程错误。我会接受你的答案,如果我可以... – user1267259
你能提供[一个最小但完整的代码示例](http://stackoverflow.com/help/mcve)?这可能有助于诊断问题。提及什么是您的操作系统,以及如何运行脚本:您希望发生什么?而发生什么呢? – jfs