python - twisted adbapi中参数tx的意思

查看:146
本文介绍了python - twisted adbapi中参数tx的意思的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

最近在做爬虫相关的工作,在数据库使用from twisted.enterprise import adbapi 导入adbapi的包,我在网上查找相关实例,我发现
好多里面都有个参数tx, 但是这个tx前后又没有说明,不懂这个tx在这里代表什么意思

实例一:在下面update_feed_seen_ids有个tx参数

def update_feed_seen_ids(self, tx):
        tx.execute(self.feed_seen_sql)
        result = tx.fetchall()
        if result:
            #id[0]是因为result的子项是tuple类型
            self.feed_ids_seen = set([int(id[0]) for id in result])
        else:
            #设置已查看过的id列表
            self.feed_ids_seen = set()

实例二:在下面def do_replace还有个tx参数

@staticmethod
    def do_replace(tx, item):
        """Does the actual REPLACE INTO"""
        sql = """REPLACE INTO properties (url, title, price, description)
        VALUES (%s,%s,%s,%s)"""
        args = (
            item["url"][0][:100],
            item["title"][0][:30],
            item["price"][0],
            item["description"][0].replace("\r\n", " ")[:30]
        )
        tx.execute(sql, args)

解决方案

已解决:dbpool.runInteraction当中的自定义函数_conditional_insert第二个参数通过看网上的实例,发现这个参数tx是自己定义的,但是不论tx被定义为什么名字,都可以执行.execute()

def _conditional_insert(self,tx,item):
        tx.execute('insert into real_time_pm(`city`) values(%s)',item['city'])

def process_item(self,item,spider):
    self.dbpool.runInteraction(self._conditional_insert,item)

这篇关于python - twisted adbapi中参数tx的意思的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆