Python多线程之间如何进行通信 threading

1 需求

需要一个爬虫,爬虫线程从互联网爬取数据,将数据爬取下来之后,在由另外一个线程将爬取的数据写入文件或数据库,两个线程同时开多个拷贝。

2 容易犯的错误

一种错误写法:

for i in range(ts):
    threads2.append(MakeData(qu=qu,fund_data_qu=fund_data_qu))
    threads.append(ThreadToFile(fund_data_qu=fund_data_qu))
for i in range(ts):
    threads2[i].start()
    threads[i].start()
for i in range(ts):
    threads[i].join()
    threads[i].join()

正确的写法应该是将其分开写:

for i in range(ts):
    threads2.append(MakeData(qu=qu,fund_data_qu=fund_data_qu))
for i in range(ts):
    threads2[i].start()
for i in range(ts):
    threads2[i].join()

for i in range(ts):
    threads.append(ThreadToFile(fund_data_qu=fund_data_qu))
for i in range(ts):
    threads[i].start()
for i in range(ts):
    threads[i].join()

你可能感兴趣的:(Python)