1 '''A multi-threaded command dispatcher.'''
2 from datetime
import datetime
3 from multiprocessing
import cpu_count
4 from threading
import Thread
, RLock
6 from logging
import info
, debug
9 class ProcesserThread(Thread
):
10 '''A thread which asynchronously processes commands from a command list.'''
12 def __init__(self
, cmd_lst
, lock
):
14 cmd_lst: commands that can be exectuted from serveral processers
15 lock: shared lock between processers for accessing the command list'''
17 self
.cmd_lst
= cmd_lst
21 '''The thread's logics.'''
26 cmd
= self
.cmd_lst
.pop(0)
27 info('%s %s' % (datetime
.now().strftime("%H:%M:%S"), cmd
))
32 '''Synchronously processes a command list.'''
34 def __init__(self
, cmd_lst
):
36 cmd_lst: the list that must be executed'''
37 self
.cmd_lst
= cmd_lst
40 '''The processer's logics.'''
41 for cmd
in self
.cmd_lst
:
42 before_str
= datetime
.now().strftime("(executing) %H:%M:%S")
43 info('%s %s' % (before_str
, cmd
))
45 after_str
= datetime
.now().strftime("(executed) %H:%M:%S")
46 debug('%s %s' % (after_str
, cmd
))
50 '''Synchronously processes commands that are submitted (eventually) using
53 def __init__(self
, cores
):
55 cores: how many cpu cores are used to process the commands'''
57 self
.cores
= cpu_count()
58 except NotImplementedError:
60 self
.cores
= cores
if cores
else int(self
.cores
/ 4 + 1)
61 debug('processer-mgr: using %s cores' % self
.cores
)
65 '''Adds cmd to the list that will be processed.'''
69 '''Performs the commands that have been added.'''
71 threads
, lock
= [], RLock()
72 threads
= [ProcesserThread(self
.cmd_lst
, lock
)
73 for _
in range(self
.cores
)]
74 [thread
.start() for thread
in threads
]
75 [thread
.join() for thread
in threads
]
77 SyncProcesser(self
.cmd_lst
).run()