[英]Control running Python Process (multiprocessing)
I have yet another question about Python multiprocessing. 我还有一个关于Python多处理的问题。 I have a module that creates a Process and just runs in a while True loop. 我有一个模块,它创建一个Process并在True True循环中运行。 This module is meant to be enabled/disabled from another Python module. 该模块旨在从另一个Python模块启用/禁用。 That other module will import the first one once and is also run as a process. 该其他模块将一次导入第一个模块,并且也将作为一个进程运行。
How would I better implement this? 我该如何更好地实现呢?
so for a reference: 所以作为参考:
#foo.py
def foo():
while True:
if enabled:
#do something
p = Process(target=foo)
p.start()
and imagine second module to be something like that: 想象第二个模块是这样的:
#bar.py
import foo, time
def bar():
while True:
foo.enable()
time.sleep(10)
foo.disable()
Process(target=bar).start()
Constantly running a process checking for condition inside a loop seems like a waste, but I would gladly accept the solution that just lets me set the enabled
value from outside . 不断地在循环内运行过程以检查条件似乎是一种浪费,但是我很乐意接受只允许我从外部设置enabled
值的解决方案。 Ideally I would prefer to be able to terminate and restart the process, again from outside of this module. 理想情况下,我希望能够再次从该模块外部终止并重新启动该过程。 From my understanding, I would use a Queue to pass commands to the Process. 据我了解,我将使用队列将命令传递给流程。 If it is indeed just that, can someone show me how to set it up in a way that I can add something to the queue from a different module. 如果确实如此,那么有人可以告诉我如何设置它,以便可以从其他模块向队列中添加内容。
Can this even be easily done with Python or is it time to abandon hope and switch to something like C or Java 可以使用Python轻松做到这一点吗?还是该放弃希望而改用C或Java之类的东西了
I purposed in comment two different approches : 我在评论中打算使用两种不同的方法:
multiprocessing.Value
使用multiprocessing.Value
的共享变量 def target_process_1(run_statement):
while True:
if run_statement.value:
print "I'm running !"
time.sleep(1)
def target_process_2(run_statement):
time.sleep(3)
print "Stoping"
run_statement.value = False
time.sleep(3)
print "Resuming"
run_statement.value = True
if __name__ == "__main__":
run_statement = Value("i", 1)
process_1 = Process(target=target_process_1, args=(run_statement,))
process_2 = Process(target=target_process_2, args=(run_statement,))
process_1.start()
process_2.start()
time.sleep(8)
process_1.terminate()
process_2.terminate()
from multiprocessing import Process
import time
import os, signal
def target_process_1():
while True:
print "Running !"
time.sleep(1)
def target_process_2(target_pid):
time.sleep(3)
os.kill(target_pid, signal.SIGSTOP)
time.sleep(3)
os.kill(target_pid, signal.SIGCONT)
if __name__ == "__main__":
process_1 = Process(target=target_process_1)
process_1.start()
process_2 = Process(target=target_process_2, args=(process_1.pid,))
process_2.start()
time.sleep(8)
process_1.terminate()
process_2.terminate()
Side note: if possible do not run a while True
. 旁注:如果可能,请不要运行while True
。
EDIT : if you want to manage your process in two different files, supposing you want to use a control by sharing a variable, this is a way to do. 编辑 :如果您想在两个不同的文件中管理您的进程,假设您想通过共享变量来使用控件,这是一种方法。
# file foo.py
from multiprocessing import Value, Process
import time
__all__ = ['start', 'stop', 'pause', 'resume']
_statement = None
_process = None
def _target(run_statement):
""" Target of the foo's process """
while True:
if run_statement.value:
print "I'm running !"
time.sleep(1)
def start():
global _process, _statement
_statement = Value("i", 1)
_process = Process(target=_target, args=(_statement,))
_process.start()
def stop():
global _process, _statement
_process.terminate()
_statement, _process = None, _process
def enable():
_statement.value = True
def disable():
_statement.value = False
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.