Show More
@@ -1,70 +1,74 b'' | |||
|
1 | 1 | # encoding: utf-8 |
|
2 | 2 | """ |
|
3 | 3 | Object for encapsulating process execution by using callbacks for stdout, |
|
4 | 4 | stderr and stdin. |
|
5 | 5 | """ |
|
6 | 6 | __docformat__ = "restructuredtext en" |
|
7 | 7 | |
|
8 | 8 | #------------------------------------------------------------------------------- |
|
9 | 9 | # Copyright (C) 2008 The IPython Development Team |
|
10 | 10 | # |
|
11 | 11 | # Distributed under the terms of the BSD License. The full license is in |
|
12 | 12 | # the file COPYING, distributed as part of this software. |
|
13 | 13 | #------------------------------------------------------------------------------- |
|
14 | 14 | |
|
15 | 15 | #------------------------------------------------------------------------------- |
|
16 | 16 | # Imports |
|
17 | 17 | #------------------------------------------------------------------------------- |
|
18 | 18 | from killableprocess import Popen, PIPE |
|
19 | 19 | from threading import Thread |
|
20 | 20 | from time import sleep |
|
21 | import os | |
|
21 | 22 | |
|
22 | 23 | class PipedProcess(Thread): |
|
23 | 24 | """ Class that encapsulates process execution by using callbacks for |
|
24 | 25 | stdout, stderr and stdin, and providing a reliable way of |
|
25 | 26 | killing it. |
|
26 | 27 | """ |
|
27 | 28 | |
|
28 | 29 | def __init__(self, command_string, out_callback, |
|
29 | 30 | end_callback=None,): |
|
30 | 31 | """ command_string: the command line executed to start the |
|
31 | 32 | process. |
|
32 | 33 | |
|
33 | 34 | out_callback: the python callable called on stdout/stderr. |
|
34 | 35 | |
|
35 | 36 | end_callback: an optional callable called when the process |
|
36 | 37 | finishes. |
|
37 | 38 | |
|
38 | 39 | These callbacks are called from a different thread as the |
|
39 | 40 | thread from which is started. |
|
40 | 41 | """ |
|
41 | 42 | self.command_string = command_string |
|
42 | 43 | self.out_callback = out_callback |
|
43 | 44 | self.end_callback = end_callback |
|
44 | 45 | Thread.__init__(self) |
|
45 | 46 | |
|
46 | 47 | |
|
47 | 48 | def run(self): |
|
48 | 49 | """ Start the process and hook up the callbacks. |
|
49 | 50 | """ |
|
51 | env = os.environ | |
|
52 | env['TERM'] = 'xterm' | |
|
50 | 53 | process = Popen((self.command_string + ' 2>&1', ), shell=True, |
|
54 | env=env, | |
|
51 | 55 | universal_newlines=True, |
|
52 | 56 | stdout=PIPE, stdin=PIPE, ) |
|
53 | 57 | self.process = process |
|
54 | 58 | while True: |
|
55 | 59 | out_char = process.stdout.read(1) |
|
56 | 60 | if out_char == '': |
|
57 | 61 | if process.poll() is not None: |
|
58 | 62 | # The process has finished |
|
59 | 63 | break |
|
60 | 64 | else: |
|
61 | 65 | # The process is not giving any interesting |
|
62 | 66 | # output. No use polling it immediatly. |
|
63 | 67 | sleep(0.1) |
|
64 | 68 | else: |
|
65 | 69 | self.out_callback(out_char) |
|
66 | 70 | |
|
67 | 71 | if self.end_callback is not None: |
|
68 | 72 | self.end_callback() |
|
69 | 73 | |
|
70 | 74 |
General Comments 0
You need to be logged in to leave comments.
Login now