Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:将函数作为参数传递和多线程_Python_Multithreading_Function Parameter - Fatal编程技术网

Python:将函数作为参数传递和多线程

Python:将函数作为参数传递和多线程,python,multithreading,function-parameter,Python,Multithreading,Function Parameter,我是python新手,我不明白为什么“无线程”类可以工作而“有线程”类不能工作 我的“With_thread”类的目的是在用函数调用新线程时启动它,这样我就可以同时执行任何函数。(我怀疑问题是由您的_目标和_参数之间的某种冲突以及线程中定义的相同属性引起的 您不需要在With thread中定义\uuuu init\uuuu()或run()。线程类拥有的init允许您传递关键字参数target和args,其run()方法将使用初始化时提供的args和kwargs运行target 因此,您可以完全

我是python新手,我不明白为什么“无线程”类可以工作而“有线程”类不能工作

我的“With_thread”类的目的是在用函数调用新线程时启动它,这样我就可以同时执行任何函数。(我怀疑问题是由您的_目标和_参数之间的某种冲突以及线程中定义的相同属性引起的

您不需要在With thread中定义
\uuuu init\uuuu()
run()
。线程类拥有的init允许您传递关键字参数
target
args
,其
run()
方法将使用初始化时提供的args和kwargs运行target

因此,您可以完全摆脱WITHREAD,然后调用:

f1 = threading.Thread(target=some_Func, args=([1,2], 6))

我尝试更改传递给线程的参数的名称,异常跳转。为了缩小问题范围,如果我打印
\u target
的类型是
'NoneType'
,它应该是
,好的,我们可以回到那一点。我的建议有效吗?如果有效,为什么要对线程进行子类化?您的解决方案很好。我只是问一下问得好,在Python2.7/x64的某些函数中,它的值应该是什么
class Without_thread():
    def __init__(self, target, *args):
        self._target = target
        self._args = args

    def foo(self):
        self._target(*self._args)
def some_Func(data, key):
    print("some_Func was called : data=%s; key=%s" % (str(data), str(key)))

f1 = With_thread(some_Func, [1,2], 6)
f1.start()
f1.join() 

f2 = Without_thread(some_Func, [1,2], 6)
f2.foo()
    self._target(*self._args)
TypeError: 'NoneType' object is not callable
some_Func was called : data=[1, 2]; key=6
f1 = threading.Thread(target=some_Func, args=([1,2], 6))