4

Python爬虫编程思想(137):多线程和多进程爬虫--Thread类与线程对象

 2 years ago
source link: https://blog.csdn.net/nokiaguy/article/details/123575537
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爬虫编程思想(137):多线程和多进程爬虫--Thread类与线程对象

蒙娜丽宁 于 2022-03-18 15:11:33 发布 472

        Thread类构造方法的target关键字参数不仅可以是一个函数,还可以是一个对象,我们可以称这个对象为线程对象。其实线程调用的仍然是函数,只是这个函数用对象进行了封装。这么做的好处是可以将于线程函数相关的代码都放在对象对应的类中,这样更能体现面向对象的封装性。

        线程对象对应的类需要有一个可以传入线程函数和参数的构造方法,而且在类中还必须有一个名为“__call__”的方法。当线程启动时,会自动调用线程对象的“__call__”方法,然后在该方法中会调用线程函数。

        下面的例子在使用Thread类的实例启动线程时,通过Thread类构造方法传入了一个线程对象,并通过线程对象指定了线程函数和相应的参数。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK