python - 无法从 Redis 订阅中获取数据?

标签 python redis callback python-asyncio

我正在尝试通过在我的客户端应用程序上使用订阅来从 Redis channel 获取数据。为此,我将 python 与 asyncio 和 aioredis 结合使用。

我想使用我的订阅在服务器上发生变化时更新我的​​主应用程序的变量,但我无法将从订阅接收到的数据传递到我的主线程。

根据 aioredis website ,我通过以下方式实现了我的订阅:

sub = await aioredis.create_redis(
     'redis://localhost')

ch1 = await sub.subscribe('channel:1')
assert isinstance(ch1, aioredis.Channel)

async def async_reader(channel, globarVar):
    while await channel.wait_message():
        msg = await channel.get(encoding='utf-8')
        # ... process message ...
        globarVar = float(msg)
        print("message in {}: {}".format(channel.name, msg))

tsk1 = asyncio.ensure_future(async_reader(ch1, upToDateValue))

但我无法更新全局变量,我猜 python 只传递当前值作为参数(我希望如此,但想确定)。

是否有任何可行的方法可以从订阅中获取数据?或者传递对我可以使用的共享变量或队列的引用?

最佳答案

你应该重新设计你的代码,这样你就不需要全局变量了。您的所有处理都应在收到消息时进行。但是,要修改全局变量,您需要使用 global 关键字在函数中声明它。您不会四处传递全局变量 - 您只是使用它们。

子:

import aioredis
import asyncio
import json

gvar = 2

# Do everything you need here or call another function
# based on the message.  Don't use a global variable.
async def process_message(msg):
  global gvar
  gvar = msg

async def async_reader(channel):
  while await channel.wait_message():
    j = await channel.get(encoding='utf-8')
    msg = json.loads(j)
    if msg == "stop":
      break
    print(gvar)
    await process_message(msg)
    print(gvar)

async def run(loop):
  sub = await aioredis.create_redis('redis://localhost')
  res = await sub.subscribe('channel:1')
  ch1 = res[0]
  assert isinstance(ch1, aioredis.Channel)

  await async_reader(ch1)

  await sub.unsubscribe('channel:1')
  sub.close()

loop = asyncio.get_event_loop()
loop.run_until_complete( run(loop) )
loop.close()

出版商:

import asyncio
import aioredis

async def main():
    pub = await aioredis.create_redis('redis://localhost')

    res = await pub.publish_json('channel:1', ["Hello", "world"])
    await asyncio.sleep(1)
    res = await pub.publish_json('channel:1', "stop")

    pub.close()


if __name__ == '__main__':
    asyncio.get_event_loop().run_until_complete(main())

关于python - 无法从 Redis 订阅中获取数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54706613/

相关文章:

python - 替换与模式匹配的部分字符串

python - 从 Gmail 下载子文件夹

python - 使用 pygerrit 从 gerrit url 获取机器人评论失败

redis - 如何在环回上设置redis

redis - 使用 Redis Pubsub 是否可以订阅 Set 的更改?

javascript - 更改回调函数名称

node.js - 如何从 Node.js 中的 mysql SELECT 查询返回值

python - 如何使用 TTX/fontTools 重命名 TrueType 字体中的字形?

redis - 通过 Sentinel 连接到 Redis 集群

c++ - 使用 std::bind 将类成员函数注册为函数的回调