class - multiprocessing

 

 


Basic

 

#

import multiprocessing

def worker(num):
    """thread worker function"""
    print 'Worker:', num
    return

if __name__ == '__main__':
    jobs = []
    for i in range(5):
        p = multiprocessing.Process(target=worker, args=(i,))
        p.start()

# A process per each test set

from subprocess import Popen
import glob
 
tests = glob.glob('test*.py')
processes = []
for test in tests:
    processes.append(Popen('python %s' % test, shell=True))
 
for process in processes:
    process.wait()

run()

Method representing the process’s activity.

start()

In multiprocessing, processes are spawned by creating a Process object and then calling its start() method.

join([timeout])

Block the calling thread until the process whose join() method is called terminates or until the optional timeout occurs.

If timeout is None then there is no timeout.

A process can be joined many times.

 


&

 

python my_selenium_process1.py &

python my_selenium_process2.py &

python my_selenium_process3.py &

 


lock

 

For instance one can use a lock to ensure that only one process prints to standard output at a time:

from multiprocessing import Process, Lock

def f(l, i):
    l.acquire()
    print 'hello world', i
    l.release()

if __name__ == '__main__':
    lock = Lock()

    for num in range(10):
        Process(target=f, args=(lock, num)).start()

 


 

 

 

Creative Commons license icon Creative Commons license icon