Multiprocessing provides both local and remote concurrency, effectively using the process (not the thread) through the global Interpretation lock (Global Interceptor Lock, GIL). Because of the GIL's presence, using multithreading in CPU-intensive programs does not effectively take advantage of multicore CPUs, because an interpreter will only have one thread executing at a time. Therefore, the multiprocessing module can fully utilize the hardware's multiprocessor to work. It supports running on Unix and Windows systems.
Example 1:
From multiprocessing import Process
def f (name):
print ' Hello ', name
if __name__ = = ' __main__ ':
p = Process (target=f, args= (' Bob ')) #使用Process创建一个进程, target represents the calling object, and args represents the positional parameter tuple of the calling object
P.start () #开始进程
P.join () #等待进程结束
Operation Result:
Hello Bob
Example 2:
From multiprocessing import Process
Import OS
def info (title):
Print title
print ' Module name: ', __name__
If hasattr (OS, ' Getppid '): # used to determine if the system contains getppid methods
print ' Parent process: ', os.getppid () #得到父进程 ID
print ' Process ID: ', os.getpid () #得到本身进程 ID
def f (name):
Info (' function f ')
print ' Hello ', name
if __name__ = = ' __main__ ':
Info (' main Line ')
p = Process (target=f, args= (' Bob ',))
P.start ()
P.join ()
Operation Result:
Main Line
Module Name: __main__
Process id:8972
function f
Module Name: __main__
Process id:10648
Hello Bob
Example 3:
We created the Thread1 and thread2 two folders and put in two test cases respectively, and we wrote the all_tests_process.py file to execute the test case through the multi-process.
#coding =utf-8
Import UnitTest, time, OS, multiprocessing
Import commands
From Email.mime.text import Mimetext
Import Htmltestrunner
Import Sys
Sys.path.append (' \selenium_proces ' Def EEEcreatsuite1 ():
Casedir=[]
Listaa=os.listdir (' d:\\selenium_proces\\ ') files/folders in #读取selenium_proces directory
For XX in Listaa: #找到文件/folder whose name contains "thread" of the file/folder added to the Casedir array
If "thread" in XX:
Casedir.append (XX)
Print Casedir
suite=[]
for n in Casedir:
Testu Nit=unittest. TestSuite ()
Discover=unittest.defaulttestloader.discover (str (n), pattern= ' start_*.py ', top_level_dir=r ' E:\\ ')
for Test_suite in Discover:
for test_case in Test_suite:
testunit.addtests (test_case)
#print testunit
Suite.append (testunit)
return suite,casedir
def eeeeemultiruncase (Suite,casedir):
now = Time.strftime ('%y-%m-%d-%h_%m_%s ', Time.localtime (Time.time ()))
filename = ' d:\\selenium_python\\report\\ ' +now+ ' result.html '
fp = file (filename, ' WB ')
Proclist=[]
S=0
For I in Suite:
Runner = Htmltestrunner.htmltestrunner (
STREAM=FP,
Title=str (Casedir[s]) +u ' Test Report ',
Description=u ' use case execution: '
)
proc = multiprocessing. Process (target=runner.run,args= (i,))
Proclist.append (proc)
S=s+1
for proc in ProcList:
Proc.start ()
for proc in ProcList:
Proc.join ()
Fp.close ()
if __name__ = = "__main__":
Runtmp=eeecreatsuite1 ()
Eeeeemultiruncase (Runtmp[0],runtmp[1])
Python multi-Process execution Automated test cases