''' 子線程的操做 multiprocessing多線程Process ''' import multiprocessing import time def worker(args,interval): print("start worker {0}".format(args)) time.sleep(interval) print("end worker {0}".format(args)) ''' p表明一個多進程 p.is_alive() #判斷進程是否存活 p.run() #啓動進程 p.start() #會自動調用run方法,推薦使用 p.join(timeout) #等待子進程結束或到超時時間 p.terminate() #強制子進程退出 p.name #進程名字 p.pid #進程pid multiprocessing cpu_count() #統計cpu總數 active_children() #得到全部子進程 ''' def main(): print("start main") # 單個元祖的元素表示(1,) p1 = multiprocessing.Process(target=worker, args=(1, 1)) p2 = multiprocessing.Process(target=worker, args=(2, 2)) p3 = multiprocessing.Process(target=worker, args=(3, 3)) p1.start() p1.join(timeout=0.5) p2.start() p3.start() print("the number of cup is: {0}".format(multiprocessing.cpu_count())) for p in multiprocessing.active_children(): print("The name of active children is : {0},pid is : {1} is alived ".format(p.name,p.pid)) print("end main") if __name__ == '__main__': main()