本文整理汇总了Python中multiprocessing.Process.wait方法的典型用法代码示例。如果您正苦于以下问题:Python Process.wait方法的具体用法?Python Process.wait怎么用?Python Process.wait使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类multiprocessing.Process
的用法示例。
在下文中一共展示了Process.wait方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from multiprocessing import Process [as 别名]
# 或者: from multiprocessing.Process import wait [as 别名]
class Viewer:
def __init__(self):
# create a pipe to communicate with the child
(self.pipe, self.child_pipe) = Pipe()
# create the subprocess
self.child = Process(target=self._handler)
# set the child to run as a background process (i.e., exit when parent does)
self.child.daemon = True
self.child.start()
def update(self, knowledge_base, robot_state):
self.pipe.send((knowledge_base, robot_state))
# path = '/tmp/transfer.pickle'
# pickle.dump((knowledge_base, robot_state), open(path, 'wb'))
# self.pipe.send(path)
def close(self):
# signal the child to close
self.pipe.close()
# wait a bit for the process to exit
self.child.wait(1)
# kill the process if it doesn't exit normally
if self.child.is_alive():
logger.warn("Viewer escalating to kill child")
self.child.cancel()
self.child = None
logger.info("Viewer closed")
def _handler(self):
logger.debug("child {} start".format(self.child.pid))
viewer = RemoteKnowledgeBaseViewer(self.child_pipe)
viewer.run()
@property
def heartbeat(self):
logger.info("checking for heartbeat")
if self.pipe.poll():
logger.info("heartbeat present")
self.pipe.recv()
logger.info("heartbeat read")
return True
else:
return False
示例2: print
# 需要导入模块: from multiprocessing import Process [as 别名]
# 或者: from multiprocessing.Process import wait [as 别名]
print('fork: \nProcess (%s) start...' % os.getpid())
pid = os.fork() # fork 轻松创建子进程
if pid == 0: # 子进程返回 0, 父进程返回子进程的 id, getppid() 得到父进程 pid
print('I am child process (%s) and my parent is (%s).' % (os.getpid(), os.getppid()))
exit(0) # 子进程执行打这里就退出, 不执行后面的
else: print('I (%s) just created a child process (%s).' % (os.getpid(), pid))
##################################################################
## multiprocessing Process
from multiprocessing import Process # fork 无法在 Windows 上运行, Process 可以跨平台
def run_proc(name): print('Run child process %s (%s)...' % (name, os.getpid())) # 子进程要执行的代码
print('\nProcess: \nParent process (%s).' % os.getpid())
p = Process(target=run_proc, args=('test',)) # 参数在 args 中传
p.start() # start() 方法启动, 这样创建进程比 fork() 还要简单
p.join() # join() 方法可以等待子进程结束后再继续往下运行, 通常用于进程间的同步
##################################################################
## subprocess
import subprocess # 启动一个子进程, 然后控制其输入和输出
print('\nsubprocess 没有控制输入输出: \n$ nslookup www.python.org') # 不用控制的
r = subprocess.call(['nslookup', 'www.python.org']); print('Exit code:', r)
print('\nsubprocess 控制输入输出: $ nslookup')
p = subprocess.Popen(['nslookup'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output, err = p.communicate(b'set q=mx\npython.org\nexit\n') # 相当于下面三条命令: set q=mx; python.org; exit
print(output.decode('utf-8'))
print('Exit code:', p.returncode)
p = subprocess.Popen(['nslookup'], stdin=sys.stdin, stdout=sys.stdout, stderr=sys.stderr)
p.wait() # 加上这句话才能在终端等待输入...
p.kill()
print(p.returncode) # 手动结束的话不会执行到这里, 执行到 kill() 后就会报错 KeyboardInterrupt
示例3: while
# 需要导入模块: from multiprocessing import Process [as 别名]
# 或者: from multiprocessing.Process import wait [as 别名]
UCINEO_NANCY_CE = [ conf+'\\UCINEO_NANCY_CE\\ApiFctSup_bouchon.obj', conf+'\\UCINEO_NANCY_CE\\ConvertUTF.obj', conf+'\\UCINEO_NANCY_CE\\HttpInterface.obj', conf+'\\UCINEO_NANCY_CE\\InterfaceSupUcineo.obj', conf+'\\UCINEO_NANCY_CE\\io_pc.obj', conf+'\\UCINEO_NANCY_CE\\Io_plug_com.obj', conf+'\\UCINEO_NANCY_CE\\Io_plug_counter.obj', conf+'\\UCINEO_NANCY_CE\\Io_plug_gain.obj', conf+'\\UCINEO_NANCY_CE\\Io_plug_input.obj', conf+'\\UCINEO_NANCY_CE\\Io_plug_intit.obj', conf+'\\UCINEO_NANCY_CE\\Io_plug_matrice.obj', conf+'\\UCINEO_NANCY_CE\\Io_plug_output.obj', conf+'\\UCINEO_NANCY_CE\\Iodlg.obj', conf+'\\UCINEO_NANCY_CE\\main.obj', conf+'\\UCINEO_NANCY_CE\\noyau_win32.obj', conf+'\\UCINEO_NANCY_CE\\SaeSupInterface_Cfg.obj' ]
while(liste):
cur = liste.pop()
objet = eval(cur).pop()
while(eval(cur)):
print objet
while True:
if( running.value < MAX_RUNNING_VALUE ):
p = Process( target = nmake, args = (cur,running,objet,config) )
p.start()
running.value = running.value + 1
objet = eval(cur).pop()
print "Il y a ",running.value," en cours"
break
print running.value
time.sleep(6)
# while ( running.value <= 0 ):
# print running.value
if(config == "1"):
cfg = 'CFG=UCINEO_NANCY_CE - Win32 (WCE x86) Release'
else:
cfg = 'CFG=UCINEO_NANCY_CE - Win32 (WCE x86) Debug'
p = subprocess.Popen(['nmake', '/F', 'UCINEO_NANCY_CE.vcn ',cfg])
p.wait()
# elapsed_time = time.time() - start_time
# print "Compilation effectuee en : "+elapsed_time.str()+" secondes"