Python - 我可以为此目的使用 blinker 库吗?
Python - Can I use blinker library for this purpose?
我想知道我是否可以使用 blinker 库(或者可能使用任何库)来完成这些工作。
- 我 运行 一个使用 Flask 的网络应用程序,在这个应用程序中(可能
app.py
),我定义了一个名为 updated 的信号(例如 blinker.signal('updated')
).
- 在一个单独的过程中,我将任何函数(我称之为
subscriber
)连接(订阅)到 updated 信号。这个进程 运行 永远像一个守护进程。
- 每当网络端发生更新时,我希望调用
subscriber
函数。
所以我写了一些代码:
app.py(烧瓶应用)
from flask import Flask
from blinker import signal
app = Flask(__name__)
updated = signal('updated')
@app.route('/update')
def update():
updated.send('nothing')
return 'Updated!'
background.py
import time
from app import updated
@updated.connect
def subscriber(*args, **kwargs):
print('An update occurred on the web side!')
while True:
print('Waiting for signals...')
time.sleep(1)
和 运行 带有 flask run
命令的 Web 应用程序。现在,当我访问 localhost:5000/update
时,我可以在浏览器中看到 Updated!
消息,但我看不到来自其他进程的消息 An update occurred on the web side!
。
是我的做法错了吗?如果是,我怎么能做这样的工作?等待您的答复,谢谢。
布林克的描述is:
A fast Python in-process signal/event dispatching system.
强调我的,所以不,你不能使用库在两个进程之间发送信号。
你可以做什么呢?好吧,Python 标准库的文档有一整章关于 Interprocess Communication and Networking. It's not clear what you're trying to build, but if you need to build a task queue (with "workers"), for instance, try Celery or TaskTiger. If you do need to actually send messages between processes, something more complex like ZeroMQ 是合适的。
我想知道我是否可以使用 blinker 库(或者可能使用任何库)来完成这些工作。
- 我 运行 一个使用 Flask 的网络应用程序,在这个应用程序中(可能
app.py
),我定义了一个名为 updated 的信号(例如blinker.signal('updated')
). - 在一个单独的过程中,我将任何函数(我称之为
subscriber
)连接(订阅)到 updated 信号。这个进程 运行 永远像一个守护进程。 - 每当网络端发生更新时,我希望调用
subscriber
函数。
所以我写了一些代码:
app.py(烧瓶应用)
from flask import Flask
from blinker import signal
app = Flask(__name__)
updated = signal('updated')
@app.route('/update')
def update():
updated.send('nothing')
return 'Updated!'
background.py
import time
from app import updated
@updated.connect
def subscriber(*args, **kwargs):
print('An update occurred on the web side!')
while True:
print('Waiting for signals...')
time.sleep(1)
和 运行 带有 flask run
命令的 Web 应用程序。现在,当我访问 localhost:5000/update
时,我可以在浏览器中看到 Updated!
消息,但我看不到来自其他进程的消息 An update occurred on the web side!
。
是我的做法错了吗?如果是,我怎么能做这样的工作?等待您的答复,谢谢。
布林克的描述is:
A fast Python in-process signal/event dispatching system.
强调我的,所以不,你不能使用库在两个进程之间发送信号。
你可以做什么呢?好吧,Python 标准库的文档有一整章关于 Interprocess Communication and Networking. It's not clear what you're trying to build, but if you need to build a task queue (with "workers"), for instance, try Celery or TaskTiger. If you do need to actually send messages between processes, something more complex like ZeroMQ 是合适的。