2

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

    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

other_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()

The result of main() is printed None, not 3, i.e., abstract class' method is called instead of child's one. (Assume all the modules are in the one place.)

Can anyone explain this behaviour? Or what is the thing I do not understand in inheritance in Python combined with threadings.Thread?

P.S. With the similar code I've met error from callback <bound method BaseClass... of < ...ChildClass...> > from websocket.WebSocketApp._callback().

P.P.S. Important to note that I use websocket-client, not websockets.

Uno Yakshi
  • 31
  • 4
  • In a 2.7 shell I get the value 3, as you were expecting. Inheritance is not affected by threading AFAIK – John Feb 28 '18 at 22:17
  • @John, sorry to mislead you: the problem might be in websockets as well. Updated the code. – Uno Yakshi Feb 28 '18 at 22:41

0 Answers0