Created
December 16, 2019 15:49
-
-
Save no1xsyzy/43c9490250d7efac0f4ba6704d26a72f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding:utf-8 | |
import requests | |
import threading | |
from queue import Queue, Empty | |
import time | |
c_thrd = int(input("请输入要执行的线程数:")) | |
# 设置队列长度 | |
workQueue = Queue(c_thrd) | |
# 线程池 | |
threads = [] | |
start = time.time() | |
END_SYM = object() | |
def crawler(threadName, q): | |
# 执行多线程 | |
# 从队列里取出数据 | |
while True: | |
j = q.get() | |
if j is END_SYM: | |
return | |
else: | |
print(f"线程{threadName}: {j}") | |
class myThread(threading.Thread): | |
def __init__(self, name, q, *args, **kwargs): | |
super().__init__(*args, **kwargs) | |
self.name = name | |
self.q = q | |
def run(self): | |
try: | |
print(self.name + "号线程启动") | |
crawler(self.name, workQueue) | |
print(self.name + "号线程结束") | |
except Exception as e: | |
print(q.qsize(), threadName, "号线程运行错误", e) | |
# 创建新线程 | |
for tName in range(c_thrd): | |
thread = myThread(tName, workQueue) | |
thread.start() | |
threads.append(thread) | |
# 读取数据,放入队列 | |
filename = "saas_user_mini.txt" | |
with open(filename, 'r', encoding='utf-8') as f: | |
for i in f: | |
workQueue.put(i) | |
for i in range(c_thrd): | |
workQueue.put(END_SYM) | |
# 等待所有线程完成 | |
for t in threads: | |
t.join() | |
end = time.time() | |
print('Queue多线程批量执行时间为:', end - start) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
a | |
b | |
c | |
d | |
e | |
f | |
g | |
h | |
j | |
i | |
k | |
l | |
m | |
n | |
o | |
p | |
q | |
r | |
s | |
t | |
u | |
v | |
w | |
x | |
y | |
z |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment