gpt4 book ai didi

python - 出现异常错误 “Exception in thread Thread-13 (most likely raised during interpreter shutdown)”

转载 作者:太空狗 更新时间:2023-10-30 00:02:35 26 4
gpt4 key购买 nike

我写了一个简单的脚本,它使用线程从服务中检索数据。

    __author__ = 'Igor'
import requests
import time
from multiprocessing.dummy import Pool as ThreadPool

ip_list = []
good_ip_list = []
bad_ip_list = []
progress = 0

with open('/tmp/ip.txt') as f:
ip_list = f.read().split()

def process_request(ip):
global progress
progress += 1
if progress % 10000 == 0:
print 'Processed ip:', progress, '...'
r = requests.get('http://*****/?ip='+ip, timeout=None)
if r.status_code == 200:
good_ip_list.append(ip)
elif r.status_code == 400:
bad_ip_list.append(ip)
else:
print 'Unknown http code received, aborting'
exit(1)

pool = ThreadPool(16)
try:
pool.map(process_request, ip_list)
except:
for name, ip_list in (('/tmp/out_good.txt', good_ip_list), ('/tmp/out_bad.txt', bad_ip_list)):
with open(name, 'w') as f:
for ip in ip_list:
print>>f, ip

但是在处理了一些请求(40k-50k)之后我收到:

线程 Thread-7 中的异常(很可能在解释器关闭期间引发):追溯(最近一次通话):进程结束,退出代码为 0

尝试更改服务设置:

        <timeout>999</timeout>
<connectionlimit>600</connectionlimit>
<httpthreads>32</httpthreads>
<workerthreads>128</workerthreads>

但还是一样的错误。谁能帮我 - 怎么了?

最佳答案

感谢所有帮助我解决这个问题的人。重写了整个代码,现在可以完美运行了:

__author__ = 'kulakov'
import requests
import time
from multiprocessing.dummy import Pool as ThreadPool

ip_list = []
good_ip_list = []
bad_ip_list = []

with open('/tmp/ip.txt') as f:
ip_list = f.read().split()

s = requests.Session()
def process_request(ip):
r = s.get('http://*****/?ip='+ip, timeout=None)
if r.status_code == 200:
# good_ip_list.append(ip)
return (ip, True)
elif r.status_code == 400:
# bad_ip_list.append(ip)
return (ip, False)
else:
print 'Unknown http code received, aborting'
exit(1)

pool = ThreadPool(16)
for ip, isOk in pool.imap(process_request, ip_list):
if isOk:
good_ip_list.append(ip)
else:
bad_ip_list.append(ip)
pool.close()
pool.join()

for name, ip_list in (('/tmp/out_good.txt', good_ip_list), ('/tmp/out_bad.txt', bad_ip_list)):
with open(name, 'w') as f:
for ip in ip_list:
print>>f, ip

一些新的有用信息:

1) 在一个函数 process_request 中的不同线程中写入数据真的是个坏主意,现在它返回 statement(true\false) 和 ip。

2) keep alive 默认完全支持requests,但是如果你想使用它,你必须创建一个对象实例 Session,并仅对其应用 get 方法:

s = requests.Session()
r = s.get('http://*****/?ip='+ip, timeout=None)

关于python - 出现异常错误 “Exception in thread Thread-13 (most likely raised during interpreter shutdown)”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29027587/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com