我有一个基于tkinter的GUI程序在Python 3.4.1中运行.我在程序中运行了几个线程来从各个URL获取JSON数据.我想添加一些WebSocket功能,以便能够允许程序充当服务器,并允许多个客户端通过WebSocket连接到它并交换其他JSON数据.
我正在尝试使用Autobahn | Python WebSocket服务器进行asyncio.
我首先尝试在GUI程序下的单独线程中运行asyncio事件循环.但是,每次尝试都会产生'AssertionError:线程'Thread-1'中没有当前事件循环.
然后,我尝试使用标准库多处理程序包生成一个进程,该程序包在另一个进程中运行asyncio事件循环.当我尝试这个时,我没有得到任何异常,但WebSocket服务器也没有启动.
甚至可以在另一个Python程序的子进程中运行asyncio事件循环吗?
有没有办法将asyncio事件循环集成到当前多线程/ tkinter程序中?
更新 下面是我尝试运行初始测试的实际代码.
from autobahn.asyncio.websocket import WebSocketServerProtocol
from autobahn.asyncio.websocket import WebSocketServerFactory
import asyncio
from multiprocessing import Process
class MyServerProtocol(WebSocketServerProtocol):
def onConnect(self, request):
print("Client connecting: {0}".format(request.peer))
def onOpen(self):
print("WebSocket connection open.")
def onMessage(self, payload, isBinary):
if isBinary:
print("Binary message received: {0} bytes".format(len(payload)))
else:
print("Text message received: {0}".format(payload.decode('utf8')))
## echo back message verbatim
self.sendMessage(payload, isBinary)
def onClose(self, wasClean, code, reason):
print("WebSocket connection closed: {0}".format(reason))
def start_server():
factory = WebSocketServerFactory("ws://10.241.142.27:6900", debug = False) …Run Code Online (Sandbox Code Playgroud) 我正在使用Python 2.7.5和Tkinter.我正在将状态消息写入文本小部件.插入我正在使用:
text_widget.insert(INSERT, 'my status here.\n')
Run Code Online (Sandbox Code Playgroud)
这很好用.但是,每个状态都会在上一个状态之后添加.如何在Text窗口小部件的顶部插入新行,以便最新状态位于顶部,同时保留以前的状态消息?
谢谢.
我有以下python字典与元组的键和值:
{(A, 1): (B, 2),
(C, 3): (D, 4),
(B, 2): (A, 1),
(D, 4): (C, 3),
}
Run Code Online (Sandbox Code Playgroud)
如何在键和值之间获得一组独特的组合?这样(A,1):(B,2)看来,不是(B,2):(A,1)吗?