-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparallel_processing.py
More file actions
39 lines (31 loc) · 1.16 KB
/
parallel_processing.py
File metadata and controls
39 lines (31 loc) · 1.16 KB
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
# https://www.google.co.in/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=run%20two%20process%20parallel%20in%20python
# http://stackoverflow.com/questions/7207309/python-how-can-i-run-python-functions-in-parallel
from multiprocessing import Process
import time
from twisted.internet import reactor
from loggers import sensor_logger
def func1():
print 'func1: starting' , time.time()
# suscribe(to_msg) or reply(to_request)
# do your task
# print 'func1: finishing', time.time()
func2('eg. log request came')
for i in xrange(10000000): pass
reactor.run()
def func2(msg='wassup'):
print 'func2: starting' , time.time() , ' message : ' , msg
# do your task
# communication can be done via pipe/queue queue , queue has genralised appliaction handlers than pipe .
# publish(msg) to suscribers with (unique topic e.g log) or (request/reply) pub sub is better .
print 'func2: finishing' ,time.time()
def runInParallel(*functions):
proc = []
for fi in functions:
p = Process(target=fi)
p.start()
proc.append(p)
for p in proc:
p.join()
if __name__ == '__main__':
# run processes as parallel
runInParallel(func1, func2)