从Python中的另一个类访问变量

时间:2018-06-28 10:17:04

标签: python multithreading sockets inheritance multiple-inheritance

我有两个类OpenInvitationAcceptinvitation我要访问self.socketIOReqAccept,这是一个从OpenInvitationAcceptinvitation的socketIO对象

OpenInvitation

from socketIO_client_nexus import SocketIO, LoggingNamespace
import threading

class OpenInvitation():
    def __init__(self):
        thread = threading.Thread(target=self.socket)
        thread.start()

    def socket(self):.
        def getSID(args):
            NOWsocketID = args
            self.socketIDfromOpenforPlay = NOWsocketID
            print(NOWsocketID)

        self.socketIOReqAccept = SocketIO('http://192.168.219.1', 5200, LoggingNamespace)
        self.socketIOReqAccept.on('s_id', getSID)
        self.socketIOReqAccept.wait()

我正在尝试从打开的Invitation类中访问self.socketIOReqAccept变量。

我在另一个线程中尝试过socketIO = self.OpenForPlay.socketIOReqAccept,但是它正在停止同时运行的GUI。

请帮助我解决这个问题。

1 个答案:

答案 0 :(得分:0)

在初始化方法中实例化self.sockerIOReqAccept

from socketIO_client_nexus import SocketIO, LoggingNamespace
import threading

class OpenInvitation():
    def __init__(self):
        self.socketIOReqAccept = SocketIO('http://192.168.219.1', 5200, LoggingNamespace)
        thread = threading.Thread(target=self.socket)
        thread.start()

    def socket(self):.
        def getSID(args):
            NOWsocketID = args
            self.socketIDfromOpenforPlay = NOWsocketID
            print(NOWsocketID)

        self.socketIOReqAccept.on('s_id', getSID)
        self.socketIOReqAccept.wait()

oi = OpenInvitation()
socker = oi.socketIOReqAccept