python线程混淆代码 'int'对象不可调用

标签 python mysql multithreading

我知道它很困惑,但是线程是如此令人困惑......我不知道问题是否出在我的语法中或者是否出在我使用的方法中...... def 会在 mysql 中插入(并且它在不是线程时工作),另一个奇怪的事情是,运行代码后我注意到行已正确插入,但我仍然得到“self._target(*self._args, **self._kwargs) ) 类型错误:'int'对象不可调用”

def thistaginsert(tagy):
    global idn
    global forbidentags
    global termcount
    tagy = tagy[0]
    if not tagy.isdigit():
        if not tagy in forbidentags:
            wordpress.execute(s_term % tagy)
            term0 = wordpress.fetchall()
            term0 = term0[0][0]
            if term0 == 0:
                tmp_i_term = i_term.format(tag1=tagy, tag2=tagy)
                wordpress.execute(tmp_i_term)
                cnx2.commit()
                tmp_s_termname = s_termname.format(buceta=tagy)
                wordpress.execute(tmp_s_termname)
                term = wordpress.fetchall()
                term = term[0]
                wordpress.execute(i_termtax % term)
                cnx2.commit()
                wordpress.execute(s_tax % term)
                tax_id = wordpress.fetchall()
                tax_id = tax_id[0][0]
                tmp_i_RL = i_RL.format(idn=idn, taxid=tax_id)
                wordpress.execute(tmp_i_RL)
                cnx2.commit()
                termcount += 1
            else:
                tmp_s_termname = s_termname.format(buceta=tagy)
                wordpress.execute(tmp_s_termname)
                term = wordpress.fetchall()
                term = term[0]
                wordpress.execute(s_tax % term)
                tax_id = wordpress.fetchall()
                tax_id = tax_id[0][0]
                tmp_i_RL = i_RL.format(idn=idn, taxid=tax_id)
                wordpress.execute(tmp_i_RL)
                cnx2.commit()
                termcount += 1
        return termcount
.
.
. #many lines later
                if tags:
                  for tag in tags:
                        ttt = Thread(target=thistaginsert(tag))
                        ttt.start()
                        threads.append(ttt)
                else:
                    print('no tags')

最佳答案

您直接调用该函数,然后将结果传递给 Thread() 构造函数作为目标函数。由于该函数返回一个 int,这就解释了错误;您正在尝试使用 int 作为线程的入口点,并且 int 不可调用。

大概您打算让函数调用发生在另一个线程上。要实现这一点,请更改以下内容:

ttt = Thread(target=thistaginsert(tag))
#                   ^
# This invokes the function and uses the result as the "target" argument.

致:

ttt = Thread(target=lambda: thistaginsert(tag))
#                   ^
# This syntax creates a new function object that will call thistaginsert(tag) when
# it is called, and that new function is what gets passed as the "target" argument.
<小时/>

正如评论中指出的,您还可以这样做:

ttt = Thread(target=thistaginsert, args=(tag,))
#                               ^ Note the lack of parens; we are passing the
#                                 function object, not calling it!

关于python线程混淆代码 'int'对象不可调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25734595/

相关文章:

python - 使用按键单击时如何更改pyqt5中的按钮文本

python - 获取 n 的所有约数的该算法的运行时间复杂度是多少?

python - 如何计算 POS 标注器的标注精度和召回率?

python - 在 Pandas 数据框中将不同的日期时间格式转换为 MM/DD/YYYY 格式

php - 收件箱sql查询解决方案

mysql - 如何在 mySQL 中定义自定义 ORDER BY 顺序

c# - '' 没有重载匹配委托(delegate) 'System.Threading.ParameterizedThreadStart'

multithreading - 检查线程是否仍在运行的可靠方法是什么?

php - 在 SQL 查询中传递大量值的最佳方法?

Java (Android) 线程 - 每隔一段时间重复任务,并访问值