如何在 Pyro5 中为服务器端创建 GUI?

How to create a GUI for the server side in Pyro5?

我正在使用 Pyro5,我想为服务器端创建一个 GUI。这个想法是服务器也可以向客户端发送消息。

我的问题是,每当客户端从服务器代码访问该方法时,它每次都会打开一个新的服务器 GUI。

示例代码如下。服务器代码在每次初始化 class.

时启动一个线程
#SERVER CODE
from Pyro5.api import expose, behavior, serve
import Pyro5.errors


# Chat box administration server.
# Handles logins, logouts, channels and nicknames, and the chatting.
@expose
@behavior(instance_mode="single")
class ChatBox(object):
    def __init__(self):
        self.channels = {}  # registered channels { channel --> (nick, client callback) list }
        self.nicks = []  # all registered nicks on this server
        gui_thread = threading.Thread(target=self.gui_loop)
        gui_thread.start()

    def gui_loop(self):
        self.win = tkinter.Tk()
        self.win.title('Server')
        self.win.configure(bg="lightgray")

    def getChannels(self):
        return list(self.channels.keys())

    def getNicks(self):
        return self.nicks

    def join(self, channel, nick, callback):
        if not channel or not nick:
            raise ValueError("invalid channel or nick name")
        if nick in self.nicks:
            raise ValueError('this nick is already in use')
        if channel not in self.channels:
            print('CREATING NEW CHANNEL %s' % channel)
            self.channels[channel] = []
        self.channels[channel].append((nick, callback))
        self.nicks.append(nick)
        print("%s JOINED %s" % (nick, channel))
        self.publish(channel, 'SERVER', '** ' + nick + ' joined **')
        return [nick for (nick, c) in self.channels[channel]]  # return all nicks in this channel

    def leave(self, channel, nick):
        if channel not in self.channels:
            print('IGNORED UNKNOWN CHANNEL %s' % channel)
            return
        for (n, c) in self.channels[channel]:
            if n == nick:
                self.channels[channel].remove((n, c))
                break
        self.publish(channel, 'SERVER', '** ' + nick + ' left **')
        if len(self.channels[channel]) < 1:
            del self.channels[channel]
            print('REMOVED CHANNEL %s' % channel)
        self.nicks.remove(nick)
        print("%s LEFT %s" % (nick, channel))

    def publish(self, channel, nick, msg):
        if channel not in self.channels:
            print('IGNORED UNKNOWN CHANNEL %s' % channel)
            return
        for (n, c) in self.channels[channel][:]:  # use a copy of the list
            c._pyroClaimOwnership()
            try:
                c.message(nick, msg)  # oneway call
            except Pyro5.errors.ConnectionClosedError:
                # connection dropped, remove the listener if it's still there
                # check for existence because other thread may have killed it already
                if (n, c) in self.channels[channel]:
                    self.channels[channel].remove((n, c))
                    print('Removed dead listener %s %s' % (n, c))

# daemon = Pyro5.server.Daemon(host="0.0.0.0", port=9090)
# ns = Pyro5.core.locate_ns()
# print("done")
serve({
    ChatBox: "example.chatbox.server"
})

#CLIENT CODE
import threading
import contextlib
from Pyro5.api import expose, oneway, Proxy, Daemon


# The daemon is running in its own thread, to be able to deal with server
# callback messages while the main thread is processing user input.

class Chatter(object):
    def __init__(self):
        self.chatbox = Proxy('PYRONAME:example.chatbox.server')
        self.abort = 0

    @expose
    @oneway
    def message(self, nick, msg):
        if nick != self.nick:
            print('[{0}] {1}'.format(nick, msg))

    def start(self):
        nicks = self.chatbox.getNicks()
        if nicks:
            print('The following people are on the server: %s' % (', '.join(nicks)))
        channels = sorted(self.chatbox.getChannels())
        if channels:
            print('The following channels already exist: %s' % (', '.join(channels)))
            self.channel = input('Choose a channel or create a new one: ').strip()
        else:
            print('The server has no active channels.')
            self.channel = input('Name for new channel: ').strip()
        self.nick = input('Choose a nickname: ').strip()
        people = self.chatbox.join(self.channel, self.nick, self)
        print('Joined channel %s as %s' % (self.channel, self.nick))
        print('People on this channel: %s' % (', '.join(people)))
        print('Ready for input! Type /quit to quit')
        try:
            with contextlib.suppress(EOFError):
                while not self.abort:
                    line = input('> ').strip()
                    if line == '/quit':
                        break
                    if line:
                        self.chatbox.publish(self.channel, self.nick, line)
        finally:
            self.chatbox.leave(self.channel, self.nick)
            self.abort = 1
            self._pyroDaemon.shutdown()


class DaemonThread(threading.Thread):
    def __init__(self, chatter):
        threading.Thread.__init__(self)
        self.chatter = chatter
        self.setDaemon(True)

    def run(self):
        with Daemon() as daemon:
            daemon.register(self.chatter)
            daemon.requestLoop(lambda: not self.chatter.abort)


chatter = Chatter()
daemonthread = DaemonThread(chatter)
daemonthread.start()
chatter.start()
print('Exit.')

最好创建一次 tkinter GUI,运行 在主线程中创建 GUI,在子线程中 运行 Pyro5 服务器:

import threading
import tkinter
from Pyro5.api import expose, behavior, Daemon, locate_ns

@expose
@behavior(instance_mode="single")
class ChatBox(object):
    def __init__(self, textbox):
        self.channels = {}  # registered channels { channel --> (nick, client callback) list }
        self.nicks = []  # all registered nicks on this server
        self.textbox = textbox

    def log(self, msg):
        self.textbox.insert('end', msg+'\n')

    def getChannels(self):
        return list(self.channels.keys())

    def getNicks(self):
        return self.nicks

    def join(self, channel, nick, callback):
        if not channel or not nick:
            raise ValueError("invalid channel or nick name")
        if nick in self.nicks:
            raise ValueError('this nick is already in use')
        if channel not in self.channels:
            self.log('CREATING NEW CHANNEL %s' % channel)
            self.channels[channel] = []
        self.channels[channel].append((nick, callback))
        self.nicks.append(nick)
        self.log("%s JOINED %s" % (nick, channel))
        self.publish(channel, 'SERVER', '** ' + nick + ' joined **')
        return [nick for (nick, c) in self.channels[channel]]  # return all nicks in this channel

    def leave(self, channel, nick):
        if channel not in self.channels:
            self.log('IGNORED UNKNOWN CHANNEL %s' % channel)
            return
        for (n, c) in self.channels[channel]:
            if n == nick:
                self.channels[channel].remove((n, c))
                break
        self.publish(channel, 'SERVER', '** ' + nick + ' left **')
        if len(self.channels[channel]) < 1:
            del self.channels[channel]
            self.log('REMOVED CHANNEL %s' % channel)
        self.nicks.remove(nick)
        self.log("%s LEFT %s" % (nick, channel))

    def publish(self, channel, nick, msg):
        if channel not in self.channels:
            self.log('IGNORED UNKNOWN CHANNEL %s' % channel)
            return
        for (n, c) in self.channels[channel][:]:  # use a copy of the list
            c._pyroClaimOwnership()
            try:
                c.message(nick, msg)  # oneway call
            except Pyro5.errors.ConnectionClosedError:
                # connection dropped, remove the listener if it's still there
                # check for existence because other thread may have killed it already
                if (n, c) in self.channels[channel]:
                    self.channels[channel].remove((n, c))
                    self.log('Removed dead listener %s %s' % (n, c))

class DaemonThread(threading.Thread):
    def __init__(self, textbox):
        super().__init__()
        self.textbox = textbox

    def run(self):
        with Daemon() as daemon:
            ns = locate_ns()
            chatbox = ChatBox(self.textbox)
            uri = daemon.register(chatbox)
            ns.register('example.chatbox.server', uri)
            print('Ready.')
            daemon.requestLoop()

# create GUI in main thread
win = tkinter.Tk()
win.title('Server')
win.configure(bg="lightgray")

textbox = tkinter.Text(win, width=80, height=20)
textbox.pack()

# start the chat server in child thread
DaemonThread(textbox).start()

win.mainloop()

请注意,我没有使用 serve(),而是手动创建和启动守护进程。