Python爬虫-第四章-1-多线程多进程并发爬取Ⅰ

时间:2023-01-15 21:56:54

多线程多进程用的方式

# Demo Describe:多线程写法

from threading import Thread # 线程
from multiprocessing import Process # 进程


# start--------1,多线程调用的方式一----------------------
def fun1():
for i in range(100):
print('fun()1,', i)


def fun2():
# 创建线程
t = Thread(target=fun1)
# 开启多线程状态,由CPU决定执行具体时间
t.start()
for i in range(100, 200):
print('fun()2,', i)


# fun2()
# end--------1,多线程调用的方式一----------------------

# start--------2,多线程调用的方式二----------------------
class MyThread(Thread):
def run(self):
for i in range(100):
print('子线程运行,', i)


# if __name__ == '__main__':
# t = MyThread()
# t.start()
# for i in range(100):
# print('main,',i)

# end--------2,多线程调用的方式二----------------------

# start--------3,多进程调用的方式一----------------------
def funProcess1():
for i in range(100):
print('子进程,', i)


# if __name__ == '__main__':
# p = Process(target=funProcess1)
# p.start()
# for i in range(100):
# print('主进程,',i)

# end--------3,多进程调用的方式一----------------------

# start--------4,线程中增加进程练习----------------------
def funProcess2(name):
for i in range(100):
print(name, i)


if __name__ == '__main__':
t1 = Thread(target=funProcess2, args=('进程一',)) # 传参必须是元组
t1.start()

t2 = Thread(target=funProcess2, args=('进程二',))
t2.start()

# end--------4,线程中增加进程练习----------------------