我正在尝试制作一个具有多线程的网络爬虫以使其更快。我想让每次执行都增加价值。但有时该值会跳过或重复自身。
import threading
num = 0
def scan():
while True:
global num
num += 1
print(num)
open('logs.txt','a').write(str(f'{num}\n'))
for x in range(500):
threading.Thread(target=scan).start()
结果:
2
2
5
5
7
8
10
10
12
13
13
13
16
17
19
19
22
23
24
25
26
28
29
29
31
32
33
34
预期结果:
1
2
3
4
5
6
7
8
9
10
犯罪嫌疑人X
当年话下
梵蒂冈之花
相关分类