在两个不同的 python 脚本之间进行通信(传递字符串)的正确方法是什么?
我有一个使用 PySide6 生成 GUI 的 ui.py 脚本,还有另一个 bot.py 脚本,它监听不和谐/电报对话并使用异步函数捕获一些关键字。两个脚本都在同一个目录中。
我已将 bot.py 文件中的 Asyncio 事件循环代码放入名为 runscript() 的函数中,并使用 ui.py 中的 multiprocessing.Process 在单击 PySide6 QPushButton 后运行该函数。
所以这里的问题是我想在我的 GUI 中显示 bot.py 捕获的关键字,所以我需要将该字符串传递给 ui.py(将需要以另一种方式传递字符串 - 从 ui.py 到 bot.py-将来),但我不知道该怎么做。我已经尝试过 multiprocessing.Pipe 但这会阻止我的代码,因为当新消息到达时(使用 Asyncio),脚本会从不和谐/电报中获取消息,我等不及要发生这种情况了。
#bot.py
# do other stuff above here
@discord_client.event
async def on_message(message):
if message.channel.id in discord_channel_list:
discord_message = message.content
selected_symbol = message_analyzer(discord_message)
print(selected_symbol)
async def discord_connection():
await discord_client.start(discord_token)
def runscript():
connection = asyncio.get_event_loop()
connection.create_task(binance_connection())
connection.create_task(discord_connection())
connection.create_task(telegram_connection())
connection.create_task(connection_check())
try:
connection.run_forever()
except KeyboardInterrupt:
print("\nShutting down...")
except:
print("\nWARN! Shutting down...")
例如,我需要获取 selected_symbol 的值并将其传输到 ui.py
#ui.py
import bot
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.start_button = QPushButton("Start")
self.start_button.clicked.connect(self.run)
def run(self):
bot_process = Process(target=bot.runscript)
bot_process.daemon = True
bot_process.start()
实现这一目标的正确方法是什么?提前致谢。