解决Pycharm 多线程时出现错误Process finished with exit code -1073741819 (0xC0000005)

港真,这个问题整的我快抑郁了,排雷花了三四天时间,网上说啥的都有。先说结论吧,最终的解决方案是把代码里用到的pandas库降低了版本,用的1.1.5版本,才没再次报该错误。教训就是:python库,尽量不要用最新版,因为不知道哪里会产生奇奇怪怪的bug!用老版本,非常稳定!

接下来细说。

事情是这样,客户要求给他写个脚本,然后因为请求量大,所以用threading多线程搞比较好一些,一次性开10000个线程+ip随机切换,不会被封本地ip。先上部分代码。

import codecs
import csv
import os
import random
import sha3
import ecdsa
from web3 import Web3
import threading
import time
import smtplib
from email.header import Header
from email.mime.text import MIMEText
import pandas as pd
import requests
from bs4 import BeautifulSoup

# 从该网站不定时抓取可用ip地址,并单独作为一个线程使用
def getip():
    try:
        for i in range(1, 10):
            if len(list(set(ip_url_list))) < 3000:
                url = 'https://www.kuaidaili.com/free/inha/' + str(i) + '/'
                headers = {}
                re = requests.get(url, headers=headers)
                soup = BeautifulSoup(re.text, 'html.parser')
                ip_datas = soup.find('table')
                df_tables = pd.read_html(str(ip_datas))
                for j in range(len(df_tables)):
                    df = df_tables[j]
                    df_ip_list = df['IP'].tolist()
                    df_port_list = df['PORT'].tolist()
                    df_http_type = df['类型'].tolist()
                    if len(df_ip_list) == len(df_port_list):
                        for i in range(len(df_ip_list)):
                            ip_url = df_http_type[i].lower() + '://' + str(df_ip_list[i]) + ':' + str(df_port_list[i])
                            ip_url_list.append(ip_url)
                print('ip_url_list:{} \n ip_url_list列表长度:{} '.format(list(set(ip_url_list)),len(list(set(ip_url_list)))))
                # df_ips = pd.DataFrame(list(set(ip_url_list)))       # 列表去重
                # df_ips.to_csv('IP代理地址.csv', encoding='utf-8', index=False)
                time.sleep(random.randint(60, 120))
    except BaseException:
        pass

# 核心代码不放了,因为与最终的报错无关,需要全部代码请私信

if __name__ == "__main__":

    # 启动IP代理线程
    ip_thread = threading.Thread(target=getip)
    ip_thread.start()

    # 私钥碰撞线程表
    threads = []
    for i in range(10000):
        thread = threading.Thread(target=hit_key)
        threads.append(thread)
    for t in range(len(threads)):
        threads[t].start()
        # threads[t].join(10)        # 保证内存稳定
        # threads.remove(threads[t])    # 保证内存稳定

起初,当产生这个报错的时候,我谷歌了一下,结果是说啥的都有,全部试了一遍,都没一点用处。只能放弃。————放弃一次😂

后来我注意到报错信息里有这么一条:

_PyEval_EvalFrameDefault returned a result with an error set

谷歌了一下发现一样说啥的都有,而且解决方案很少,全网找不到可用的解决方案。只能放弃。————放弃两次😂

紧接着我又仔细阅读了报错信息,发现一个Unable set什么的错误,但把多线程加到10000后,这个错误很常见————放弃三次😂

然后我把windows电脑的.py文件拷贝到mac电脑,发现代码能正常运行,一切正常,这就说明是windows电脑配置、环境或者pycharm库的问题。

为了避免是多线程库带来的问题,我把if下面的所有代码注释掉,使用while 1循环来处理,看看不用threading会不会报错,结果发现如果不用多线程,在windows上运行一切正常。

if __name__ == "__main__":
    """
    # 启动IP代理线程
    ip_thread = threading.Thread(target=getip)
    ip_thread.start()

    # 私钥碰撞线程表
    threads = []
    for i in range(10000):
        thread = threading.Thread(target=hit_key)
        threads.append(thread)
    for t in range(len(threads)):
        threads[t].start()
        # threads[t].join(10)        # 保证内存稳定,防止线程内存增加
        # threads.remove(threads[t])    # 保证内存稳定,防止线程内存增加
    """
    while 1:
    	hitkey()

这就奇怪了,这说明在windows上,threading这个库可能和其他库冲突了,因为不用threading的时候一切正常。

直到今晚,想起来对比一下mac电脑上的pycharm里的库的版本和windows上的,先把numpy库版本降下来,运行时提示必须提高numpy版本才能用当前版本的pandas,然后我就把pandas的版本又换成和mac的一样,点击运行,发现终于不报错了!!

以上就是解决这个报错的心路历程,一度给我整的快哭了,尤其是同样的代码在mac上正常运行,windows上一直报错。😭

最终结论:
如果使用pycharm运行代码报 Process finished with exit code -1073741819 (0xC0000005) 错误,优先对代码里相关的引用库进行版本降级,大概率能解决问题。

!!!----------------------看到有朋友说不会降库版本,补充pycharm中对引用库进行降版本操作-----------------------!!!

第一种,命令行在pycharm中使用teminal终端降库版本,方法网上搜;
第二种,pycharm界面,安装pandas指定版本库操作流程如下:

  1. windows版本,点击File,点击settings
    在这里插入图片描述
    2.按流程操作
    在这里插入图片描述

3.选中pandas库,双击点开
在这里插入图片描述
4.点击选中Specify viersion在这里插入图片描述
5.选中1.5.0版本,点击install package安装1.5.0版本的pandas库
在这里插入图片描述
6.完成。其他库如法炮制。

  • 8
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爬吧爬吧

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值