首页 > 数据库 >scrapy中运行一段时间报错pymysql.err.InterfaceError: (0, '')

scrapy中运行一段时间报错pymysql.err.InterfaceError: (0, '')

时间:2023-12-25 16:22:05浏览次数:45  
标签:err InterfaceError envs self dbpool item 报错 home packages

错误信息

   Traceback (most recent call last):
  File "/home/anaconda3/envs/python36/lib/python3.6/site-packages/twisted/python/threadpool.py", line 250, in inContext
    result = inContext.theWork()
  File "/home/anaconda3/envs/python36/lib/python3.6/site-packages/twisted/python/threadpool.py", line 266, in <lambda>
    inContext.theWork = lambda: context.call(ctx, func, *args, **kw)
  File "/home/anaconda3/envs/python36/lib/python3.6/site-packages/twisted/python/context.py", line 122, in callWithContext
    return self.currentContext().callWithContext(ctx, func, *args, **kw)
  File "/home/anaconda3/envs/python36/lib/python3.6/site-packages/twisted/python/context.py", line 85, in callWithContext
    return func(*args,**kw)
--- <exception caught here> ---
  File "/home/anaconda3/envs/python36/lib/python3.6/site-packages/twisted/enterprise/adbapi.py", line 474, in _runInteraction
    conn.rollback()
  File "/home/anaconda3/envs/python36/lib/python3.6/site-packages/twisted/enterprise/adbapi.py", line 52, in rollback
    self._connection.rollback()
  File "/home/anaconda3/envs/python36/lib/python3.6/site-packages/pymysql/connections.py", line 431, in rollback
    self._execute_command(COMMAND.COM_QUERY, "ROLLBACK")
  File "/home/anaconda3/envs/python36/lib/python3.6/site-packages/pymysql/connections.py", line 745, in _execute_command
    raise err.InterfaceError("(0, '')")
pymysql.err.InterfaceError: (0, '')

 翻了一下日志信息,发现有一个特点,就是在报错之前的信息大多都是抓取listing的信息,而没有返回item,也没有item入库的日志打印,所以我怀疑是adbapi的连接池中的连接很久没有使用导致连接被mysql销毁,插入的时候失败,然后事务回滚,连接池中数据库连接异常导致回滚失败,最后得到这个报错信息。

我item pipeline中使用数据库连接方法为:

    # pipeline默认调用
    def process_item(self, item, spider):
        query = self.dbpool.runInteraction(self._conditional_insert, item)
        query.addErrback(self._handle_error, item, spider)
        return item

 最后我修改了一下,每次插入数据的时候ping一下,如果重连失败就将整个数据库连接池初始化一遍,完整的代码为

class MyPipeline(object):

    def __init__(self, dbpool):
        self.dbpool = dbpool

    @classmethod
    def from_settings(cls, settings):
        dbpool = MysqlConnectionPool().dbpool()
        return cls(dbpool)

    # pipeline默认调用
    def process_item(self, item, spider):
        query = self.dbpool.runInteraction(self._conditional_insert, item)
        query.addErrback(self._handle_error, item, spider)
        return item

    def _handle_error(self, failue, item, spider):
        print(failue)

    def _conditional_insert(self, transction, item):
        tt = transction._connection._connection
        try:
            tt.ping()
        except:
            self.dbpool.close()
            self.dbpool = MysqlConnectionPool().dbpool()
        sql = """insert INTO `DOC_BASEINFO`(doc_type,author_org )
        VALUES (%s,%s)"""
        params = (
            item['doc_type'], item['author_org'])
        transction.execute(sql, params)

 来源:http://www.shanhubei.com/archives/3416.html

标签:err,InterfaceError,envs,self,dbpool,item,报错,home,packages
From: https://www.cnblogs.com/shanhubei/p/17926333.html

相关文章

  • Windows7运行谷歌浏览器Chrome报错
    问题描述Windows7安装了最新版的谷歌浏览器Chrome,每次打开直接报错:“chrome.exe-无法找到入口无法定位程序输入点SetProcessMitigationPolicy于动态链接库KERNEL32.DLL上” 问题分析更新了GoogleChrome110版本后,在Windows7上运行GoogleChrome,每次都会弹出这个......
  • Docker安装运行报错wsl问题排查方案
    Docker运行报错wsl问题排查方案在window中安装运行DockerDesktop时容易遇到有关wsl的报错问题,这里给出几个排查解决的方法用于记录。排查步骤如下:......
  • Django报错 (django.core.exceptions.ImproperlyConfigured)
    报错种类django.core.exceptions.ImproperlyConfigured:ErrorloadingMySQLdbmodule.Didyouinstallmysqlclient解决方案:1、按照mysql包pip3install-ihttp://pip.baidu.com/pypi/simple--trusted-hostpip.baidu.compymysql2、在项目同名的模块下的__init__.py文件......
  • Caused by: java.sql.SQLSyntaxErrorException: You have an error in your SQL synta
    问题描述Causedby:java.sql.SQLSyntaxErrorException:YouhaveanerrorinyourSQLsyntax;checkthemanualthatcorrespondstoyourMySQLserverversionfortherightsyntaxtousenear'orderwherestatus!='已完成''atline1问题解决最后发现时因为......
  • error: failed to push some refs to 'http://192.168.1.37:1080/nongzi/nongzi-apple
    当你直接在github上在线修改了代码,或者是直接向某个库中添加文件,但是没有对本地库同步,接着你想push上传到远程库,就会失败,  这个问题是因为远程库与本地库不一致造成的,那么我们把远程库同步到本地库就可以了先把自己代码暂存,然后再拉取更新,然后提交代码 也可参考 http......
  • Q:以非root用户编辑定时任务报错You are not allowed to use this program(crontab)
    编辑定时删除文件任务时报错crontab-e编辑定时任务时报错,如下图所示问题原因:/etc/cron.allow中没有添加对应的用户名解决办法:切换到root用户,在/etc/cron.allow中添加对应的用户名编辑定时任务:crontab-e #每天中午13点,定时删除/var路径内30天以上的文件或者文件夹013*......
  • Git:Cherry-Pick 桃色陷阱
    以下内容为本人的学习笔记,如需要转载,请声明原文链接微信公众号「ENG八戒」https://mp.weixin.qq.com/s/J2b130UVF0aPoHM9-U9UuwCherry-Pick咋一看这名字就很时髦,用来干啥呢?Cherry-Pick是什么?假设在开发某些功能或者修复bug的时候把代码commit到了错误的分支A,并且分支A......
  • spring:Exception in thread "main" java.lang.NoClassDefFoundError: org/springframe
     设置了父类框架<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.1.10.RELEASE</version><relativePath/><!--l......
  • spring:Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/comm
    Java运行时环境中找不到org.apache.commons.logging.LogFactory这个类。  在maven中导入依赖即可 <dependency><groupId>commons-logging</groupId><artifactId>commons-logging</artifactId><version>1.2</version><!-......
  • Linux 启动MySQL 报错:Status:“Server shutdown complete“ Error:13(权限不够)
    Status:“Servershutdowncomplete”Error:13(权限不够)我在银河麒麟V10系统中安装启动MySQL出现的一个错误:“Servershutdowncomplete”Error:13(权限不够),我在其他系统中没有遇到到。问题背景在国产化系统银河麒麟V10上尝试启动MySQL服务器时,可能会收到如下错误信息:Status:"Ser......