python threading confusing code 'int' object is not callable

泄露秘密 提交于 2020-06-01 04:58:31

问题


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')

回答1:


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.

To:

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!


来源:https://stackoverflow.com/questions/25734595/python-threading-confusing-code-int-object-is-not-callable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!