Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- import os, sys
- import time
- import Queue
- import subprocess as sp
- import multiprocessing as mp
- py_exe = sys.executable
- py_scr = os.curdir+'/test.py'
- tm = 5
- param = {'t1': tm, 't2': tm, 't3': tm, 't4': tm}
- def worker(cmd=None, queue=None, myname=None):
- if not cmd or not queue or not myname: return False
- runned = True
- #err = False
- while runned:
- try:
- run = sp.Popen(cmd, cwd=os.curdir, shell=False, stdout=sp.PIPE, creationflags=8)
- #if err: queue.join_thread()
- except:
- queue.put('err')
- runned = False
- break
- while True:
- p = run.poll()
- time.sleep(0.1)
- queue.put(str(p))
- # continue
- if p == 0: # normal finish
- queue.put('zzz')
- time.sleep(0.1)
- queue.put(None)
- #queue.put('0-'+str(p))
- runned = False
- break
- elif p == 1:
- queue.put('restart...')
- #err = True
- break
- #run.wait()
- # time.sleep(3)
- # run = sp.Popen(cmd, cwd=os.curdir, shell=False, stdout=sp.PIPE, creationflags=8)
- # time.sleep(0.1)
- # p = run.poll()
- # queue.put('1-'+str(p))
- else:
- out = run.stdout.readline()
- queue.put(out.rstrip())
- #queue.put('e-'+str(p))
- def main():
- cmd = [py_exe,py_scr,'t1',str(tm)]
- q = mp.Queue()
- print cmd
- z = mp.Process(target=worker, args=(cmd, q, 'i',))
- z.start()
- while True:
- try:
- data = q.get_nowait()
- except Queue.Empty:
- continue
- if data: print data
- else: break
- z.join()
- print 'exit'
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment