1. 程式人生 > >python 線程,進程28原則

python 線程,進程28原則

div proc run fun 子線程 AS sel IT eth

先上框架

from threading import Thread
from multiprocessing import Process


class MyThread(Thread):
    def __init__(self, func):
        super(MyThread,self).__init__()
        self.func = func 

    def run(self):
        self.func()
class MyProcess(Process):
    def __init__(self, func):
        super(MyProcess,self).__init__()
        self.func = func 

    def run(self):
        self.func() 

  

傳入類的入口函數,即可實現多線程

baidu = Baidu()

sogou = Sogou()

google = Google()



baiduThread = MyThread(baidu.run)

sogouThread = MyThread(sogou.run)

googleThread = MyThread(google.run)

# 線程開啟

baiduThread.start()

sogouThread.start()

googleThread.start()

# 主線程等待子線程

baiduThread.join()

sogouThread.join()

googleThread.join()

  

python 線程,進程28原則