【问题标题】:PubNub and Python's multiprocessing.Process not compatible?PubNub 和 Python 的 multiprocessing.Process 不兼容?
【发布时间】:2018-08-04 16:04:45
【问题描述】:

我正在尝试使用 Python 的 multiprocessing.Process 包(Raspbian 9 上的 Python 3.5 - Stretch)在进程内接收来自 PubNub 的消息。

以下代码可以作为独立程序完美运行,也可以在使用 Python 线程包的线程中完美运行。但是,它不适用于 multiprocessing.Process。

是我遗漏了什么还是 PubNub 的 SubscribeListener 与 Python 的多处理包不兼容?

#!/usr/bin/env python3

from pubnub.pubnub import PubNub, SubscribeListener
from pubnub.pnconfiguration import PNConfiguration

import multiprocessing
import time

def PN_func():
    pnconfig = PNConfiguration()
    pnconfig.subscribe_key = 'sub-mykey'
    pubnub = PubNub(pnconfig)

    print('Pubnub multiprocess subscriber initiated...')

    class Listener(SubscribeListener):
        def message(self, pubnub, data):
            print("From Multiprocess function message: ", data.message)

    pubnub.add_listener(Listener())
    pubnub.subscribe().channels('my_channel').execute()

if __name__ == '__main__':
    mp = multiprocessing.Process(target=PN_func)
    mp.start()
    mp.join()

【问题讨论】:

  • 你能比不起作用更具体吗?您收到错误消息吗?您期望什么行为以及您看到什么行为?
  • @BoarGules 我有一个单独的程序,它每四秒生成一条消息。如果我在线程或独立进程下运行上述(当然使用我的个人子密钥),我会收到我的启动打印消息,然后每四秒打印一次消息。以上只是将我的启动消息打印到终端“Pubnub 多进程订阅者启动...”,然后程序终止返回 Linux shell 提示符。没有出现错误信息。
  • @FarNorth 我测试了你的代码,确实是的,它在使用多处理时退出。但是,在没有 Multiprocessing 的情况下运行时它不存在。
  • 添加的答案应该可以很好地满足您的需求。如果缺少任何内容,请告诉我,我们可以添加更多详细信息。

标签: python python-multiprocessing pubnub


【解决方案1】:

PubNub 和 Python 多处理

我无法让 SDK 在多处理工作器中处理同步请求。然而,以下技巧非常有效:

同时使用多处理和 PubNub

简单的example.py文件包含以下代码:

import multiprocessing
import requests

SUB_KEY  = 'demo'
CHANNELS = ['my_channel']

def main():
    mp = multiprocessing.Process(target=subscriber)
    mp.start()
    mp.join()

def subscriber():
    timetoken = '0' ## pointer to last message received
    while True:
        url = "/".join([
            'https://ps.pubnub.com/subscribe'
        ,   SUB_KEY
        ,   ",".join(CHANNELS)
        ,   '0'
        ,   timetoken
        ])

        print(url)

        response  = requests.get(url)
        data      = response.json()
        messages  = data[0]
        timetoken = data[1]

        print(data)

if __name__ == '__main__': main()

多处理子进程的输出

> python example.py 
https://ps.pubnub.com/subscribe/demo/my_channel/0/0
[[], u'15336927707090912']
https://ps.pubnub.com/subscribe/demo/my_channel/0/15336927707090912
[[{u'text': u'hey'}], u'15336927943808959']
https://ps.pubnub.com/subscribe/demo/my_channel/0/15336927943808959
[[{u'text': u'hey'}], u'15336927945476647']
https://ps.pubnub.com/subscribe/demo/my_channel/0/15336927945476647
[[{u'text': u'hey'}], u'15336927946996529']
https://ps.pubnub.com/subscribe/demo/my_channel/0/15336927946996529
[[{u'text': u'hey'}], u'15336927948441519']
https://ps.pubnub.com/subscribe/demo/my_channel/0/15336927948441519
[[{u'text': u'hey'}], u'15336927950007602']
https://ps.pubnub.com/subscribe/demo/my_channel/0/15336927950007602

【讨论】:

  • @Stephen_Blum 感谢您用有效的解决方案回复我。是否有计划更新 SubscribeListener 以使用 multiprocessing.Process?
  • 这与 request_syncrequest_async 有关。我将添加有关该错误的详细信息并提交到 git repo。
  • 这已提交,我们将努力提供补丁。
猜你喜欢
  • 2012-01-08
  • 1970-01-01
  • 2013-03-19
  • 1970-01-01
  • 1970-01-01
  • 2014-03-12
  • 1970-01-01
  • 2022-06-14
  • 2017-12-30
相关资源
最近更新 更多