Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/337.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.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
Python3中的简单线程_Python_Python 3.x_Python 2.7_Python Multithreading - Fatal编程技术网

Python3中的简单线程

Python3中的简单线程,python,python-3.x,python-2.7,python-multithreading,Python,Python 3.x,Python 2.7,Python Multithreading,在Python2中,我使用这种简单的方式运行线程,并通过args传递参数: import threading class FuncThread(threading.Thread): ''' it worked fine in Python2 ''' def __init__(self, target, *args): self._target = target self._args = args prin

在Python2中,我使用这种简单的方式运行线程,并通过args传递参数:

import threading


class FuncThread(threading.Thread):
    '''
        it worked fine in Python2
    '''
    def __init__(self, target, *args):
        self._target = target
        self._args = args
        print( self._args )
        threading.Thread.__init__(self)
    def run(self, *args):
      print( self._args )
      self._target(*self._args)

def testThreading(say=''):
  print("I'm a thread %s" % say)

t = FuncThread(testThreading, 'hi')
t.start()
现在在Python3中,这已经不起作用了,我开始

Exception in thread Thread-1:
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/threading.py", line 916, in _bootstrap_inner
    self.run()
  File "main.py", line 11, in run
    self._target(*self._args)
TypeError: 'NoneType' object is not callable
因为在运行中重写self.\u参数为空。如果我在Python3中使用新语法,它是

# this works fine in Python3
threading.Thread(target=testThreading, args=('Hello Thread!',)).start()
这样可以正常工作,那么如何正确覆盖run方法呢?

请按如下方式尝试:

import threading


class FuncThread(threading.Thread):

    def __init__(self, target, *args):
      threading.Thread.__init__(self)
      self._target = target
      self._args = args
      print( self._args )

    def run(self, *args):
      print( self._args )
      self._target(*self._args)

def testThreading(say=''):
  print("I'm a thread %s" % say)

t = FuncThread(testThreading, 'hi')
t.start()
我以前也遇到过这样的情况,在对子类进行任何尝试之前初始化父类,在这种情况下,FuncThread最终被覆盖。

请按如下方式尝试:

import threading


class FuncThread(threading.Thread):

    def __init__(self, target, *args):
      threading.Thread.__init__(self)
      self._target = target
      self._args = args
      print( self._args )

    def run(self, *args):
      print( self._args )
      self._target(*self._args)

def testThreading(say=''):
  print("I'm a thread %s" % say)

t = FuncThread(testThreading, 'hi')
t.start()

我以前也遇到过这样的情况,在对子类进行任何尝试之前初始化父类,在这种情况下,FuncThread最终被覆盖。

这是Python3的一个解决方法:

class FuncThread(threading.Thread):
    def __init__(self, target, *args):
        self._xtarget = target
        self._args = args
        print( self._args )
        threading.Thread.__init__(self)
    def run(self, *args):
      print( self._args )
      self._xtarget(*self._args)

这是Python3的解决方案:

class FuncThread(threading.Thread):
    def __init__(self, target, *args):
        self._xtarget = target
        self._args = args
        print( self._args )
        threading.Thread.__init__(self)
    def run(self, *args):
      print( self._args )
      self._xtarget(*self._args)
基本threading.Thread类使用self.\u target和self.\u args用于自己的目的。因为您在调用super _uinit _uu时没有参数,所以在父构造函数中将这些参数设置为None。要解决此问题,只需在创建实例时删除您的_init__; use关键字args,并让默认行为为您完成任务:

import threading

class FuncThread(threading.Thread):
    def run(self, *args):
      print( self._args )
      self._target(*self._args)

def testThreading(say=''):
  print("I'm a thread %s" % say)

t = FuncThread(target=testThreading, args=('hi',))
t.start()
如果要保留原始构造函数签名,请使用target和args参数调用parent _init_uu,在这种情况下,您不需要自己显式设置它们:

import threading

class FuncThread(threading.Thread):
    def __init__(self, target, *args):
        super().__init__(target=target, args=args)
    def run(self, *args):
      print( self._args )
      self._target(*self._args)

def testThreading(say=''):
  print("I'm a thread %s" % say)

t = FuncThread(testThreading, 'hi')
t.start()
基本threading.Thread类使用self.\u target和self.\u args用于自己的目的。因为您在调用super _uinit _uu时没有参数,所以在父构造函数中将这些参数设置为None。要解决此问题,只需在创建实例时删除您的_init__; use关键字args,并让默认行为为您完成任务:

import threading

class FuncThread(threading.Thread):
    def run(self, *args):
      print( self._args )
      self._target(*self._args)

def testThreading(say=''):
  print("I'm a thread %s" % say)

t = FuncThread(target=testThreading, args=('hi',))
t.start()
如果要保留原始构造函数签名,请使用target和args参数调用parent _init_uu,在这种情况下,您不需要自己显式设置它们:

import threading

class FuncThread(threading.Thread):
    def __init__(self, target, *args):
        super().__init__(target=target, args=args)
    def run(self, *args):
      print( self._args )
      self._target(*self._args)

def testThreading(say=''):
  print("I'm a thread %s" % say)

t = FuncThread(testThreading, 'hi')
t.start()

它看起来像self。_target是None。@quamrana是的,因为Python3中底层线程库中发生了一些更改。添加一个工作版本,但没有我的类。明白了。在任何其他句子之前的_init__。它看起来像self。_target是None。@quamrana是的,因为Python3中的底层线程库中发生了一些更改。添加一个工作版本,但没有我的类。明白了。在任何其他句子之前的uuu init uuuu。