【问题标题】:How to know process interrupted in python?如何知道python中的进程中断?
【发布时间】:2017-10-23 07:42:08
【问题描述】:

从我的调试日志中,我发现我的进程被中断了,我在我的代码中设置了try-catch,如下所示,

         try:
            print "update query : ........"
            query = ("Update Logs set id  = "+str(id)+" where Sid = '"+str(id)+"'")
            cursor.execute(query)
            conn.commit()
         except (MySQLdb.Error, MySQLdb.Warning) as e:
            sqlerrlogs = open("/log/mysqlerr.log","a")
            sqlerrlogs.write(str(datetime.now()))
            sqlerrlogs.write('\n')
            sqlerrlogs.write(e)
            sqlerrlogs.write('\n\n')

但是从我的日志中,在打印“更新查询:........”之后,我的日志中没有错误,如何修复我的代码以了解发生了什么? 我的python版本是2.7

【问题讨论】:

  • 你是如何运行你的进程的?
  • 每两分钟运行一次 cron
  • 你真的不想在你的数据上使用字符串插值,你很容易受到 SQL 注入攻击。使用查询参数(而不是 " + str(value) + " 使用占位符 %s 并将一系列值作为第二个参数传递给 cursor.execute())。
  • 您是否考虑过where Sid = ? 查询根本不匹配任何行的可能性?你怎么知道有什么东西被打断了?
  • Sid 值是存在的,因为从调试日志中,在执行更新查询之前,我有 print "update query : Update Logs set id = "+str(id)+" where Sid = '" +str(id)+"'"

标签: python mysql try-catch


【解决方案1】:

您可以捕获所有错误,而不仅仅是与 MySQLdb 相关的错误:

import traceback
try:
    print "update query : ........"
    query = ("Update Logs set id  = "+str(id)+" where Sid = '"+str(id)+"'")
    cursor.execute(query)
    conn.commit()
except:
    err = traceback.format_exc()
    sqlerrlogs = open("/log/mysqlerr.log","a")
    sqlerrlogs.write(str(datetime.now()))
    sqlerrlogs.write('\n')
    sqlerrlogs.write(err)
    sqlerrlogs.write('\n\n')

【讨论】:

    猜你喜欢
    • 2015-12-20
    • 1970-01-01
    • 2021-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-05
    • 2021-04-28
    • 2015-01-05
    相关资源
    最近更新 更多