python线程混淆代码'int'对象不可调用 [英] python threading confusing code 'int' object is not callable

查看:39
本文介绍了python线程混淆代码'int'对象不可调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道它很乱,但是线程太混乱了……我不知道问题出在我的语法中还是出在我使用的方法中……def 在 mysql 中插入(并且它在没有线程时工作),另一件奇怪的事情是,在运行代码后,我注意到行被正确插入,但我仍然得到self._target(*self._args, **self._kwargs)类型错误:int"对象不可调用"

its messy i know, but the threading is so confusing ...i dont know if the problem is in my sintax or if it is in the method im using... the def do inserts in mysql (and its working when is not threading),another strange thing is that after runing the code i noticed rows are inserted correctly but i still get "self._target(*self._args, **self._kwargs) TypeError: 'int' object is not callable"

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 不可调用.

You are directly calling the function and then passing the result to the Thread() constructor as the target function. Since the function returns an int, that explains the error; you are trying to use an int as the thread's entry point, and an int is not callable.

大概你打算让函数调用发生在另一个线程上.要做到这一点,请更改此内容:

Presumably you intended to have the function invocation happen on another thread. To make that happen, change this:

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.

<小时>

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


As pointed out in the comments, you can also do this:

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

这篇关于python线程混淆代码'int'对象不可调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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