Created
July 31, 2014 03:04
-
-
Save santiavenda2/487cf7900669b814d836 to your computer and use it in GitHub Desktop.
A Bottle web app to create and terminate processes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from bottle import Bottle | |
from multiprocessing import Process | |
app = Bottle() | |
processes = {} | |
@app.post('/execution') | |
def create_process(): | |
p = Process(group=None, target=infinite_proccess, name="infinite", args=(), kwargs={}) | |
p.daemon = True | |
p.start() | |
processes[p.pid] = p | |
return {'process_id': p.pid} | |
@app.delete('/execution/<processid>') | |
def kill_process(processid): | |
pid = int(processid) | |
if pid in processes: | |
p = processes[pid] | |
if p.is_alive(): | |
p.terminate() | |
while p.is_alive(): | |
print "Is alive" | |
p.join() | |
del processes[pid] | |
return {'process_id': p.pid, 'exitcode': p.exitcode} | |
@app.get('/execution') | |
def get_executions(): | |
process_json = dict([(p.pid, p.is_alive()) for pid, p in processes.iteritems()]) | |
return {'processes': process_json} | |
def infinite_proccess(): | |
import time | |
i = 0 | |
while True: | |
time.sleep(10) | |
print i |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment