SHOW:
|
|
- or go back to the newest paste.
1 | # -*- coding: utf-8 -*- | |
2 | ||
3 | import os, sys | |
4 | import time | |
5 | import Queue | |
6 | import subprocess as sp | |
7 | import multiprocessing as mp | |
8 | ||
9 | py_exe = sys.executable | |
10 | py_scr = os.curdir+'/test.py' | |
11 | ||
12 | tm = 5 | |
13 | param = {'t1': tm, 't2': tm, 't3': tm, 't4': tm} | |
14 | ||
15 | ||
16 | def worker(cmd=None, queue=None, myname=None): | |
17 | if not cmd or not queue or not myname: return False | |
18 | runned = True | |
19 | #err = False | |
20 | while runned: | |
21 | try: | |
22 | run = sp.Popen(cmd, cwd=os.curdir, shell=False, stdout=sp.PIPE, creationflags=8) | |
23 | #if err: queue.join_thread() | |
24 | except: | |
25 | queue.put('err') | |
26 | runned = False | |
27 | break | |
28 | ||
29 | while True: | |
30 | p = run.poll() | |
31 | time.sleep(0.1) | |
32 | queue.put(str(p)) | |
33 | # continue | |
34 | if p == 0: # normal finish | |
35 | queue.put('zzz') | |
36 | time.sleep(0.1) | |
37 | queue.put(None) | |
38 | #queue.put('0-'+str(p)) | |
39 | runned = False | |
40 | break | |
41 | elif p == 1: | |
42 | queue.put('restart...') | |
43 | - | err = True |
43 | + | #err = True |
44 | break | |
45 | #run.wait() | |
46 | # time.sleep(3) | |
47 | # run = sp.Popen(cmd, cwd=os.curdir, shell=False, stdout=sp.PIPE, creationflags=8) | |
48 | # time.sleep(0.1) | |
49 | # p = run.poll() | |
50 | # queue.put('1-'+str(p)) | |
51 | else: | |
52 | out = run.stdout.readline() | |
53 | queue.put(out.rstrip()) | |
54 | #queue.put('e-'+str(p)) | |
55 | ||
56 | ||
57 | def main(): | |
58 | cmd = [py_exe,py_scr,'t1',str(tm)] | |
59 | q = mp.Queue() | |
60 | print cmd | |
61 | ||
62 | z = mp.Process(target=worker, args=(cmd, q, 'i',)) | |
63 | z.start() | |
64 | ||
65 | while True: | |
66 | try: | |
67 | data = q.get_nowait() | |
68 | except Queue.Empty: | |
69 | continue | |
70 | ||
71 | if data: print data | |
72 | else: break | |
73 | ||
74 | z.join() | |
75 | print 'exit' | |
76 | ||
77 | ||
78 | if __name__ == '__main__': | |
79 | main() |