python 线程,进程28原则

先上框架

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()

  

猜你喜欢

转载自www.cnblogs.com/zenan/p/9188521.html