Show More
@@ -2,31 +2,61 b'' | |||
|
2 | 2 | |
|
3 | 3 | requires python 2.4 (or separate 'subprocess' module |
|
4 | 4 | |
|
5 | At the moment this is in a very "unhelpful" form, will be extended in the future. | |
|
6 | ||
|
7 | Usage: | |
|
8 | ||
|
9 | [ipython]|2> import jobctrl | |
|
10 | [ipython]|3> &ls | |
|
11 | <3> <jobctrl.IpyPopen object at 0x00D87FD0> | |
|
12 |
[ipython]| |
|
|
13 | -----------> _3.go() | |
|
14 | ChangeLog | |
|
15 | IPython | |
|
16 | MANIFEST.in | |
|
17 | README | |
|
18 | README_Windows.txt | |
|
19 | ||
|
20 | ... | |
|
5 | This provides 2 features, launching background jobs and killing foreground jobs from another IPython instance. | |
|
6 | ||
|
7 | Launching background jobs: | |
|
8 | ||
|
9 | Usage: | |
|
10 | ||
|
11 | [ipython]|2> import jobctrl | |
|
12 | [ipython]|3> &ls | |
|
13 | <3> <jobctrl.IpyPopen object at 0x00D87FD0> | |
|
14 | [ipython]|4> _3.go | |
|
15 | -----------> _3.go() | |
|
16 | ChangeLog | |
|
17 | IPython | |
|
18 | MANIFEST.in | |
|
19 | README | |
|
20 | README_Windows.txt | |
|
21 | ||
|
22 | ... | |
|
23 | ||
|
24 | Killing foreground tasks: | |
|
25 | ||
|
26 | Launch IPython instance, run a blocking command: | |
|
27 | ||
|
28 | [Q:/ipython]|1> import jobctrl | |
|
29 | [Q:/ipython]|2> cat | |
|
30 | ||
|
31 | Now launch a new IPython prompt and kill the process: | |
|
32 | ||
|
33 | IPython 0.8.3.svn.r2919 [on Py 2.5] | |
|
34 | [Q:/ipython]|1> import jobctrl | |
|
35 | [Q:/ipython]|2> %tasks | |
|
36 | 6020: 'cat ' (Q:\ipython) | |
|
37 | [Q:/ipython]|3> %kill | |
|
38 | SUCCESS: The process with PID 6020 has been terminated. | |
|
39 | [Q:/ipython]|4> | |
|
40 | ||
|
41 | (you don't need to specify PID for %kill if only one task is running) | |
|
21 | 42 | """ |
|
22 | 43 | |
|
23 | 44 | from subprocess import Popen,PIPE |
|
24 | import os,shlex | |
|
45 | import os,shlex,sys,time | |
|
25 | 46 | |
|
26 | 47 | from IPython import genutils |
|
27 | 48 | |
|
28 | 49 | import IPython.ipapi |
|
29 | 50 | |
|
51 | if os.name == 'nt': | |
|
52 | def kill_process(pid): | |
|
53 | os.system('taskkill /F /PID %d' % pid) | |
|
54 | else: | |
|
55 | def kill_process(pid): | |
|
56 | os.system('kill -9 %d' % pid) | |
|
57 | ||
|
58 | ||
|
59 | ||
|
30 | 60 | class IpyPopen(Popen): |
|
31 | 61 | def go(self): |
|
32 | 62 | print self.communicate()[0] |
@@ -34,8 +64,7 b' class IpyPopen(Popen):' | |||
|
34 | 64 | return '<IPython job "%s" PID=%d>' % (self.line, self.pid) |
|
35 | 65 | |
|
36 | 66 | def kill(self): |
|
37 | assert os.name == 'nt' # xxx add posix version | |
|
38 | os.system('taskkill /PID %d' % self.pid) | |
|
67 | kill_process(self.pid) | |
|
39 | 68 | |
|
40 | 69 | def startjob(job): |
|
41 | 70 | p = IpyPopen(shlex.split(job), stdout=PIPE, shell = False) |
@@ -51,11 +80,82 b' def jobctrl_prefilter_f(self,line):' | |||
|
51 | 80 | |
|
52 | 81 | raise IPython.ipapi.TryNext |
|
53 | 82 | |
|
83 | ||
|
84 | def job_list(ip): | |
|
85 | keys = ip.db.keys('tasks/*') | |
|
86 | ents = [ip.db[k] for k in keys] | |
|
87 | return ents | |
|
88 | ||
|
89 | def magic_tasks(self,line): | |
|
90 | """ Show a list of tasks. | |
|
91 | ||
|
92 | A 'task' is a process that has been started in IPython when 'jobctrl' extension is enabled. | |
|
93 | Tasks can be killed with %kill. | |
|
94 | """ | |
|
95 | ip = self.getapi() | |
|
96 | ents = job_list(ip) | |
|
97 | if not ents: | |
|
98 | print "No tasks running" | |
|
99 | for pid,cmd,cwd,t in ents: | |
|
100 | print "%d: '%s' (%s)" % (pid,cmd,cwd) | |
|
101 | ||
|
102 | def magic_kill(self,line): | |
|
103 | """ Kill a task | |
|
104 | ||
|
105 | Without args, either kill one task (if only one running) or show list (if many) | |
|
106 | With arg, assume it's the process id. | |
|
107 | ||
|
108 | %kill is typically (much) more powerful than trying to terminate a process with ctrl+C. | |
|
109 | """ | |
|
110 | ip = self.getapi() | |
|
111 | jobs = job_list(ip) | |
|
112 | ||
|
113 | if not line.strip(): | |
|
114 | if len(jobs) == 1: | |
|
115 | kill_process(jobs[0][0]) | |
|
116 | else: | |
|
117 | magic_tasks(self,line) | |
|
118 | return | |
|
119 | ||
|
120 | try: | |
|
121 | pid = int(line) | |
|
122 | kill_process(pid) | |
|
123 | except ValueError: | |
|
124 | magic_tasks(self,line) | |
|
125 | ||
|
126 | if sys.platform == 'win32': | |
|
127 | shell_internal_commands = 'break chcp cls copy ctty date del erase dir md mkdir path prompt rd rmdir time type ver vol'.split() | |
|
128 | else: | |
|
129 | # todo linux commands | |
|
130 | shell_internal_commands = [] | |
|
131 | ||
|
132 | ||
|
133 | def jobctrl_shellcmd(ip,cmd): | |
|
134 | """ os.system replacement that stores process info to db['tasks/t1234'] """ | |
|
135 | cmdname = cmd.split(None,1)[0] | |
|
136 | if cmdname in shell_internal_commands: | |
|
137 | use_shell = True | |
|
138 | else: | |
|
139 | use_shell = False | |
|
140 | ||
|
141 | p = Popen(cmd,shell = use_shell) | |
|
142 | jobentry = 'tasks/t' + str(p.pid) | |
|
143 | ||
|
144 | try: | |
|
145 | ip.db[jobentry] = (p.pid,cmd,os.getcwd(),time.time()) | |
|
146 | p.communicate() | |
|
147 | finally: | |
|
148 | del ip.db[jobentry] | |
|
149 | ||
|
150 | ||
|
54 | 151 | def install(): |
|
55 | 152 | global ip |
|
56 | 153 | ip = IPython.ipapi.get() |
|
57 | 154 | # needed to make startjob visible as _ip.startjob('blah') |
|
58 | 155 | ip.startjob = startjob |
|
59 |
ip.set_hook('input_prefilter', jobctrl_prefilter_f) |
|
|
156 | ip.set_hook('input_prefilter', jobctrl_prefilter_f) | |
|
157 | ip.set_hook('shell_hook', jobctrl_shellcmd) | |
|
158 | ip.expose_magic('kill',magic_kill) | |
|
159 | ip.expose_magic('tasks',magic_tasks) | |
|
60 | 160 | |
|
61 | install() No newline at end of file | |
|
161 | install() |
General Comments 0
You need to be logged in to leave comments.
Login now