Salome HOME
cht version
[tools/eficas.git] / OldCodes / ProcessOutputs_Eficas / EssaiMulti.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 import os
4 import sys
5 from multiprocessing import Process
6 from multiprocessing import Lock
7 from multiprocessing import Pool
8
9 def worker(num):
10     """thread worker function"""
11     print 'Worker:', num
12     return
13
14 def test1():
15     for i in range(5):
16         p = Process(target=worker, args=(i,))
17         p.start()
18         p.join()
19
20 def info(title):
21     print(title)
22     print('parent process:', os.getppid())
23     print('process id:', os.getpid())
24
25 def f(name):
26     info('function f')
27     print('hello', name)
28
29 def test2():
30     info('test2')
31     p = Process(target=f, args=('pascale',))
32     p.start()
33     p.join()
34
35 def f(l, i):
36     l.acquire()
37     try:
38         print('hello world', i)
39     finally:
40         l.release()
41
42
43 def test3(lock):
44     for num in range(10):
45         Process(target=f, args=(lock, num)).start()
46
47     # sorties evt desordonnees
48     for i in range(50):
49         p = Process(target=worker, args=(i,)).start()
50
51 def g(x):
52     return x*x
53
54
55 def test4():
56     import traceback
57     traceback.print_stack()
58     num_cores = 4
59     FolderPath='tmp'
60
61     monPool=Pool(maxtasksperchild=1) #create a multiprocessing.Pool object
62     for l in range(num_cores):
63         print(" lct on core "+str(l) )
64         p= monPool.apply_async(g,(l,))
65
66     res = monPool.apply_async(g, (20,))      # runs in *only* one process
67     print res.get(timeout=1)                 # prints "400"
68
69     # evaluate "os.getpid()" asynchronously
70     res = monPool.apply_async(os.getpid, ()) # runs in *only* one process
71     print res.get(timeout=1)              # prints the PID of that process
72
73     # launching multiple evaluations asynchronously *may* use more processes
74     multiple_results = [monPool.apply_async(os.getpid, ()) for i in range(4)]
75     print [res.get(timeout=1) for res in multiple_results]
76
77
78
79 if __name__ == '__main__':
80    #test1()
81
82    #test2()
83    
84    #lock = Lock()
85    #test3(lock)
86    print ('je suis dans main du run')
87    test4()
88