4

Python爬虫编程思想(138):多线程和多进程爬虫--从Thread类继承

 2 years ago
source link: https://blog.csdn.net/nokiaguy/article/details/123575697
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Python爬虫编程思想(138):多线程和多进程爬虫--从Thread类继承

蒙娜丽宁 于 2022-03-18 15:16:24 发布 1256

        为了更好地对与线程有关的代码进行封装,可以从Thread类派生一个子类。然后将与线程有关的代码都放到这个类中。Thread类的子类的使用方法与Thread相同。从Thread类继承最简单的方式是在子类的构造方法中通过super()函数调用父类的构造方法,并传入相应的参数值。

        下面的例子编写一个从Thread类继承的子类MyThread,并重写了父类的构造方法和run方法。最后通过MyThread类创建并启动了两个线程,并使用join方法等待着两个线程结束后再退出程序


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK