Control running Python Process (multiprocessing) -


i have yet question python multiprocessing. have module creates process , runs in while true loop. module meant enabled/disabled python module. other module import first 1 once , run process.

how better implement this?

so reference:

#foo.py  def foo():   while true:     if enabled:       #do  p = process(target=foo) p.start() 

and imagine second module that:

#bar.py import foo, time  def bar():   while true:     foo.enable()     time.sleep(10)     foo.disable()  process(target=bar).start() 

constantly running process checking condition inside loop seems waste, gladly accept solution lets me set enabled value outside. ideally prefer able terminate , restart process, again outside of module. understanding, use queue pass commands process. if indeed that, can show me how set in way can add queue different module.

can done python or time abandon hope , switch c or java

i purposed in comment 2 different approches :

  • using shared variable multiprocessing.value
  • pause / resume process signals

control sharing 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 sending 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 not run while true.


edit: if want manage process in 2 different files, supposing want use control sharing variable, way do.

# file foo.py  multiprocessing import value, process import time  __all__ = ['start', 'stop', 'pause', 'resume']  _statement = none _process = none  def _target(run_statement):     """ target of 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 

Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -