简体   繁体   中英

Unable to get result of pubnub.time() in Python

How do I get the result of pubnub.time() when called? This is definitely related to me not fully knowing python that well...

I'm trying to call pubnub.time() to test when network connectivity restores so pubnub can reconnect and I can resubscribe to the necessary channel. I can't seem to get the desired output. Per the pubnub docs , time() has 3 methods that will return an int, string, or date.

envelope = pubnub.time()

envelope.int()  outputs error
envelope.str()  outputs error
envelope.date_time()  outputs error

The only thing that I can get to work is print(envelope) which yields <pubnub.endpoints.time.Time object at 0x75fb8f50> .

I won't delve into how many iterations of things I've tried on this, and come begging for help.

UPDATE

We have a couple remote systems that we're wanting to use PubNub to gather data points from hardware that's connected to the machine. Data requests will come in, and PubNub needs to respond to the requests. Our main issue is that these remote locations have very flaky DSL that goes down for hours to days at a time, and while our python app is still running, PubNub does not reconnect. Trying reconnect_policy with LINEAR and EXPONENTIAL both fail to reconnect after an internet outage. Small, short outages will recover though. Aside from this, PubNub works great for this setup.

Here's my setup config

pnconfig = PNConfiguration()
pnconfig.subscribe_key = 'sub'
pnconfig.publish_key = 'pub'
pnconfig.reconnect_policy = 'PNReconnectionPolicy.EXPONENTIAL'
pnconfig.daemon = True
pnconfig.ssl = True

Here's our subscribe call back:

class MySubscribeCallback(SubscribeCallback):
    def presence(self, pubnub, presence):
        pass

    def status(self, pubnub, status):
        if status.is_error():
            logger.error("PN Error" + str(status.error_data.exception))

        if status.category == 
            PNStatusCategory.PNUnexpectedDisconnectCategory:
            logger.error("PN Unexpected Disconnect")
            pubnub.reconnect()
            pass

        elif status.category == PNStatusCategory.PNConnectedCategory:
            logger.info("PN Connected")

        elif status.category == PNStatusCategory.PNReconnectedCategory:
            logger.warning("PN Re-Connected")
            pubnub.subscribe().channels('devChannel').execute()
            pass

        elif status.category == PNStatusCategory.PNDecryptionErrorCategory:
            logger.error("PN Decryption Error")
            pass

PubNub SDK Reconnect on Network Error

PubNub SDKs are built durable and are able to maintain socket connections during unstable network conditions. While connectivity may not always be available, as soon as network connectivity returns, the PubNub SDK should automatically recreate the socket and download any missed messages. The PubNub Python SDK should be following this philosophy.

In your example code it appears you are setting the reconnect_policy . However your code is setting the reconnect policy variable to a string of 'PNReconnectionPolicy.EXPONENTIAL' rather than the enum PNReconnectionPolicy.EXPONENTIAL .

Try the following modification to your code to leverage the reconnection policy:

from pubnub.enums import PNReconnectionPolicy       ## <-- Import Enumerator
from pubnub.pnconfiguration import PNConfiguration

## ... init ...

## Set Reconnection Policy using Enumerator
pnconf.reconnect_policy = PNReconnectionPolicy.EXPONENTIAL

The SDK's default is pnconf.reconnect_policy = PNReconnectionPolicy.NONE . Another valid option is pnconf.reconnect_policy = PNReconnectionPolicy.LINEAR .

Set to PNReconnectionPolicy.LINEAR for automatic reconnects. Use option PNReconnectionPolicy.NONE to disable automatic reconnects. Use option PNReconnectionPolicy.EXPONENTIAL to set exponential retry interval.

Reference material: https://github.com/pubnub/python/search?q=PNReconnectionPolicy&unscoped_q=PNReconnectionPolicy

Documentation material: https://www.pubnub.com/docs/python/api-reference-configuration#configuration-args-1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM