简体   繁体   English

控制正在运行的Python进程(多处理)

[英]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 : 我在评论中打算使用两种不同的方法:

  • using a shared variable from multiprocessing.Value 使用multiprocessing.Value的共享变量
  • pause / resume the process with signals 用信号暂停/恢复过程

Control by sharing a variable 通过共享变量进行控制

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()

Control by sending a signal 通过发送信号进行控制

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.

相关问题 Python 多处理运行进程两次? - Python Multiprocessing running process twice? python中的多处理,多个进程运行相同的指令 - Multiprocessing in python, multiple process running same instructions 带有pandas的Python多重处理并非所有进程都同时运行 - Python multiprocessing with pandas not all process running at once Python多处理:只有一个进程正在运行 - Python Multiprocessing: Only one process is running Python多重处理:如何关闭一个长期运行的过程,该过程是一系列操作 - Python multiprocessing: How to shutdown a long running process that is a sequence of actions 使用 multiprocessing.Process 在 Python 中并行运行 function - Running a function in Python in Parallel using multiprocessing.Process 从python模块多处理和kivy锁运行进程而没有答案 - Running a process from python module multiprocessing and kivy locks without answer python multiprocessing.Pool kill * specific * long running或hung进程 - python multiprocessing.Pool kill *specific* long running or hung process python multiprocessing - 将子进程日志发送到在父进程中运行的 GUI - python multiprocessing - sending child process logging to GUI running in parent Python 多处理:在父进程退出后运行进程 - Python multiprocessing: Running a process after its parent exited
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM