Multithreading Python3 threadings.Thread回调错误(针对抽象方法时)
base_class.pyMultithreading Python3 threadings.Thread回调错误(针对抽象方法时),multithreading,websocket,callback,abstract-class,python-3.6,Multithreading,Websocket,Callback,Abstract Class,Python 3.6,base_class.py import threading import websocket from abc import ABCMeta, abstractmethod class A: __metaclass__ = ABCMeta def __init__(self): self.some_var = 0 @abstractmethod def failed_method(self): pass d
import threading
import websocket
from abc import ABCMeta, abstractmethod
class A:
__metaclass__ = ABCMeta
def __init__(self):
self.some_var = 0
@abstractmethod
def failed_method(self):
pass
def on_websocket_message(self, ws, msg):
var = self.failed_method()
print(var)
...
def open_websocket(self):
ws = websocket.WebSocketApp('http://some_url.com',
on_message=self.on_websocket_message, ...)
ws.run_forever()
def callback_method(self):
websocket_thread = threading.Thread(target=self.open_websocket, name='some_websocket_name')
another_var = self.failed_method()
print('Another variable\'s value is [{}]'.format(another_var))
child_class.py
from base_class import A
class B(A):
def failed_method(self):
return 3
其他_class.py
import threading
from child_class import B
def main():
child_class_instance = B()
some_thread = threadings.Thread(target=child_class_instance.callback_method, name='some_name')
some_thread.start()
main()
的结果打印为None
,而不是3
,即调用抽象类的方法而不是子类的方法。(假设所有模块都在一个位置。)
有人能解释这种行为吗?或者,在Python继承与threadings.Thread结合的过程中,我不理解的是什么
另外,我在websocket.WebSocketApp.\u callback()
中遇到了类似的代码
p.p.S.重要的是要注意,我使用的是websocket客户端
,而不是websocket
在2.7 shell中,我得到的值是3,正如您所期望的那样。继承不受线程的影响AFAIK@John,很抱歉误导您:问题可能也在WebSocket中。更新了代码。