Skip to content

Commit 1f92dd6

Browse files
author
罗晟
committed
multiprocessing
1 parent 2b3f211 commit 1f92dd6

27 files changed

+450
-35
lines changed
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# __author__ = 'admin'
2+
# from multiprocessing import Process
3+
# def worker():
4+
# print 'worder'
5+
# return
6+
# if __name__ == '__main__':
7+
# jobs = []
8+
# for i in range(5):
9+
# p = Process(target=worker)
10+
# jobs.append(p)
11+
# p.start()
12+
13+
14+
15+
from multiprocessing import Process
16+
17+
def f(name):
18+
print 'hello', name
19+
20+
if __name__ == '__main__':
21+
p = Process(target=f, args=('bob',))
22+
p.start()
23+
p.join()
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
__author__ = 'admin'
2+
import multiprocessing
3+
def worker():
4+
print 'Worker'
5+
return
6+
if __name__ == '__main__':
7+
jobs = []
8+
for i in range(5):
9+
p = multiprocessing.Process(target=worker)
10+
jobs.append(p)
11+
p.start()
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
__author__ = 'admin'
2+
import multiprocessing
3+
def worker(num):
4+
print 'worker:',num
5+
return
6+
if __name__ == '__main__':
7+
jobs = []
8+
for i in range(5):
9+
p = multiprocessing.Process(target=worker, args=(i,))
10+
jobs.append(p)
11+
p.start()
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
__author__ = 'admin'
2+
import multiprocessing
3+
import time
4+
import sys
5+
6+
def daemon():
7+
p = multiprocessing.current_process()
8+
print 'Starting:', p.name, p.pid
9+
sys.stdout.flush()
10+
time.sleep(2)
11+
print 'Exiting:',p.name, p.pid
12+
sys.stdout.flush()
13+
14+
def non_daemon():
15+
p = multiprocessing.current_process()
16+
print 'starting',p.name,p.pid
17+
sys.stdout.flush()
18+
print 'Exting:',p.name,p.pid
19+
sys.stdout.flush()
20+
21+
22+
if __name__ == '__main__':
23+
d = multiprocessing.Process(name='daemon', target=daemon)
24+
d.daemon = True #no block the main process
25+
26+
n = multiprocessing.Process(name='non_daemon', target=non_daemon)
27+
n.daemon = False #block the main process
28+
29+
d.start()
30+
time.sleep(1)
31+
n.start()

LP4E-examples/multiprocessing_exit.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
__author__ = 'admin'
2+
import multiprocessing
3+
import sys
4+
import time
5+
def exit_error():
6+
sys.exit(1)
7+
8+
def exit_ok():
9+
return 0
10+
11+
def return_value():
12+
return 0
13+
14+
def raises():
15+
raise RuntimeError('There was na error!') #except error code 1
16+
17+
def terminated():
18+
time.sleep(2)
19+
20+
if __name__ == '__main__':
21+
jobs = []
22+
for f in [exit_error, exit_ok, return_value,raises,terminated]:
23+
print 'Starting process for', f.func_name
24+
j = multiprocessing.Process(target =f, name =f.func_name)
25+
jobs.append(j)
26+
j.start()
27+
28+
jobs[-1].terminate()
29+
30+
for j in jobs:
31+
j.join()
32+
print '%15s.exitcode = %s' % (j.name, j.exitcode)
33+
34+
35+
36+
37+
38+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
__author__ = 'admin'
2+
import multiprocessing
3+
import multiprocessing_import_worker
4+
5+
if __name__ == '__main__':
6+
jobs = []
7+
for i in range(5):
8+
p = multiprocessing.Process(
9+
target = multiprocessing_import_worker.worker,
10+
)
11+
jobs.append(p)
12+
p.start()

0 commit comments

Comments
 (0)