python并发编程之开启线程的两种方式

第一种:

from multiprocessing import Process
from threading import Thread
import time
class MyThread(Thread):
    def run(self):
        print('%s is running' %self.name)
        time.sleep(3)
if __name__ == '__main__':
    t = MyThread()
    t.start()
    print('这是一个主线程')

第二种

from multiprocessing import Process
from threading import Thread
import time

def task(name):
    print('%s is running'%name)
    time.sleep(3)

if __name__ == '__main__':
    t = Thread(target=task,args=('egon',))
    # t = Process(target=task,args=('egon',))
    t.start()
    print('这是主线程')

猜你喜欢

转载自www.cnblogs.com/mayite/p/8952484.html