-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtaskmanager.py
43 lines (38 loc) · 1.67 KB
/
taskmanager.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
#========Asyschronosly manage tasks============================================#
# Creates a queue of tasks to complete one by one in a thread
# Helpfull Multithreading Resource:
# http://www.troyfawkes.com/learn-python-multithreading-queues-basics
#==============================================================================#
import cherrypy, threading, time, uuid, logging
from queue import Queue
from utilities import Switch
class TaskManager:
# Initialise the object
def __init__(self):
self.q = Queue(maxsize=0)
thread = threading.Thread(target=self.loop)
thread.start()
# Continuously loop through tasks that need to be completed
def loop(self):
# General Application loop
while True:
mytime = str(time.time())
thistask = self.q.get()
# Lets check what type of task we have
for case in Switch(thistask['type']):
# Create a new node
if case('parse_submission'):
cherrypy.config['model'].parse_submission(thistask['data'])
break
# A test so we can easily track things
if case('test'):
break
logging.debug("COMPLETED "+thistask['type']+" UID: "+thistask['uid']+" START:"+thistask['start']+" FIN: "+mytime)
self.q.task_done()
# Add a new task to the queue in the form of: {'type':'create_node','data':data}
def add(self, task):
task['uid'] = str( uuid.uuid1() )
logging.debug('ADDED NEW TASK TO THE QUEUE: '+task['uid'])
task['start'] = str(time.time())
self.q.put(task)
return task['uid']