我知道它很困惑,但是线程是如此令人困惑......我不知道问题是否出在我的语法中或者是否出在我使用的方法中...... 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/