【问题标题】:MultiThreading Python Issue(Segmentation fault)多线程 Python 问题(分段错误)
【发布时间】:2021-05-07 21:04:25
【问题描述】:

我正在尝试在多线程中运行以下代码,但是我不断收到“分段错误(核心转储)”。请告知我做错了什么-

def insert_api(r):
    url = url_responses+'/'+str(r[0])
    response = requests.get(url,headers={'api-key':APIToken,'Content-Type': 'application/json'})
    if response.status_code == 200:
        dd = json.loads(response.content)
        InsertTable('API_Response',str(r[0]),str(r[1]),json.dumps(dd['result']))        
    else:
        logMsg('','HTTP request for '+url_responses+' failed. HTTP response code is: '+str(response.status_code),'failure')
        subject='API Request failed *********                  '+ datetime.now().strftime("%Y%m%d-%H%M")
        Body='This email is to notify that the API request for the URL: ' +str(url)+' failed at '+ datetime.now().strftime("%Y%m%d-%H%M")
        email_notifier(subject,Body)

with concurrent.futures.ThreadPoolExecutor() as executor:
    executor.map(insert_api,response_list)


InsertTable 是一个函数,用于在作为参数传递的表(API_Response)中插入记录以及其他值。 email_notifier 是一个在异常情况下发送电子邮件的功能。由于我在 API 中有 95k+ 条记录,因此尝试实现多线程逻辑。

谢谢 萨米!!

【问题讨论】:

    标签: python multithreading segmentation-fault


    【解决方案1】:

    我能够通过在 InsertTable 函数之前添加锁定逻辑来解决它

    【讨论】:

    • 你能分享一个你如何解决这个问题的例子吗?我正在做几乎相同的例程,并在随机时间后得到“分段错误”。我也在将记录保存到数据库中,所以想知道它是否可能与您的问题相同。
    猜你喜欢
    • 2015-11-08
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    • 2016-02-09
    • 2019-05-15
    • 2015-09-10
    • 1970-01-01
    相关资源
    最近更新 更多