/
workserver.py
67 lines (49 loc) · 1.66 KB
/
workserver.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# workserver.py - simple HTTP server with a do_work / stop_work API
# GET /do_work activates a worker thread which uses CPU
# GET /stop_work signals worker thread to stop
import math
import socket
import threading
import time
from bottle import route, run
hostname = socket.gethostname()
hostport = 9000
keepworking = False # boolean to switch worker thread on or off
# thread which maximizes CPU usage while the keepWorking global is True
def workerthread():
# outer loop to run while waiting
while (True):
# main loop to thrash the CPI
while (keepworking == True):
for x in range(1, 69):
y = math.factorial(x)
time.sleep(3)
# start the worker thread
worker_thread = threading.Thread(target=workerthread, args=())
worker_thread.start()
def writebody():
body = '<html><head><title>Work interface - build</title></head>'
body += '<body><h2>Worker interface on ' + hostname + '</h2><ul><h3>'
if keepworking == False:
body += '<br/>Worker thread is not running. <a href="./do_work">Start work</a><br/>'
else:
body += '<br/>Worker thread is running. <a href="./stop_work">Stop work</a><br/>'
body += '<br/>Usage:<br/><br/>/do_work = start worker thread<br/>/stop_work = stop worker thread<br/>'
body += '</h3></ul></body></html>'
return body
@route('/')
def root():
return writebody()
@route('/do_work')
def do_work():
global keepworking
# start worker thread
keepworking = True
return writebody()
@route('/stop_work')
def stop_work():
global keepworking
# stop worker thread
keepworking = False
return writebody()
run(host=hostname, port=hostport)