关于python爬虫中的一些问题--8 线程启动的几种方式

129 阅读1分钟

前言

在python编程中经常要开启线程,线程的开启方式可能随着程序员的喜好不同,或者程序设计的不同而需要不同的开启形式

开启线程的几种方式

import threading,_thread
def action(i):
    print(i * 5)
#带有状态的子类
class Mythread(threading.Thread):
    def  __init__(self, i):
        self.i = i
        threading.Thread.__init__(self)
    def run(self):
        print(self.i ** 32)
Mythread(2).start()

#传入行为
thread = threading.Thread(target=(lambda: action(2))
thread.start()

#不封装lambda
thread.Thread(target=action, args=(2,)).start()

#基本的线程模块
_thread.start_new_thread(action,(2,))