PyAPN 和在发送之间休眠的需要 [英] PyAPNs and the need to Sleep between Sends

查看:17
本文介绍了PyAPN 和在发送之间休眠的需要的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 PyAPN 向 iOS 设备发送通知.我经常一次发送多组通知.如果任何令牌因任何原因损坏,该过程将停止.因此,我使用了增强的设置和以下方法:

I am using PyAPNs to send notifications to iOS devices. I am often sending groups of notifications at once. If any of the tokens is bad for any reason, the process will stop. As a result I am using the enhanced setup and the following method:

apns.gateway_server.register_response_listener

我用它来跟踪问题是哪个令牌,然后我从那里接收并发送其余的令牌.问题是,发送时捕获这些错误的唯一方法是在令牌发送之间使用睡眠定时器.例如:

I use this to track which token was the problem and then I pick up from there sending the rest. The issue is that when sending the only way to trap these errors is to use a sleep timer between token sends. For example:

for x in self.retryAPNList:
   apns.gateway_server.send_notification(x, payload, identifier = token)
   time.sleep(0.5)

如果我不使用睡眠定时器,则不会捕获到任何错误,因此我的整个 APN 列表不会被发送到,因为当存在错误令牌时进程会停止.然而,这个睡眠定时器有点随意.有时 0.5 秒就足够了,而有时我不得不将其设置为 1.在没有添加睡眠延迟的情况下,它在任何情况下都无法正常工作.这样做会减慢网络呼叫的速度,并且进入随机睡眠时间感觉不太好.

If I don't use a sleep timer no errors are caught and thus my entire APN list is not sent to as the process stops when there is a bad token. However, this sleep timer is somewhat arbitrary. Sometimes the .5 seconds is enough while other times I have had to set it to 1. In no case has it worked without some sleep delay being added. Doing this slows down web calls and it feels less than bullet proof to enter random sleep times.

对于如何在 APN 调用之间没有延迟的情况下如何工作有任何建议,或者是否有所需延迟的最佳实践?

Any suggestions for how this can work without a delay between APN calls or is there a best practice for the delay needed?

根据下面的请求添加更多代码.这是我用来控制这个的类中的 3 个方法:

Adding more code due to the request made below. Here are 3 methods inside of a class that I use to control this:

class PushAdmin(webapp2.RequestHandler):

    retryAPNList=[]
    channelID=""
    channelName = ""
    userName=""

    apns = APNs(use_sandbox=True,cert_file="mycert.pem", key_file="mykey.pem", enhanced=True)

    def devChannelPush(self,channel,name,sendAlerts):
        ucs = UsedChannelStore()
        pus = PushUpdateStore()
        channelName = ""
        refreshApnList = pus.getAPN(channel)
        if sendAlerts:
            alertApnList,channelName = ucs.getAPN(channel)
            if not alertApnList: alertApnList=[]
            if not refreshApnList: refreshApnList=[]
            pushApnList = list(set(alertApnList+refreshApnList))
        elif refreshApnList:
            pushApnList = refreshApnList
        else:
            pushApnList = []

        self.retryAPNList = pushApnList
        self.channelID = channel
        self.channelName = channelName
        self.userName = name
        self.retryAPNPush()

    def retryAPNPush(self):
        token = -1
        payload = Payload(alert="A message from " +self.userName+ " posted to "+self.channelName, sound="default", badge=1, custom={"channel":self.channelID})

        if len(self.retryAPNList)>0:
            token +=1
            for x in self.retryAPNList:
                    self.apns.gateway_server.send_notification(x, payload, identifier = token)
                    time.sleep(0.5)

下面是调用类(为了减少不相关的项目而缩写):

Below is the calling class (abbreviate to reduce non-related items):

class ChannelStore(ndb.Model):
   def writeMessage(self,ID,name,message,imageKey,fileKey):
        notify = PushAdmin()

        notify.devChannelPush(ID,name,True)

以下是我对睡眠定时器的放置所做的细微更改,似乎已解决了该问题.然而,我仍然担心给定的时间是否在所有情况下都是正确的.

Below is the slight change I made to the placement of the sleep timer that seems to have resolved the issue. I am, however, still concerned for whether the time given will be the right amount in all circumstances.

def retryAPNPush(self):
        identifier = 1
        token = -1
        payload = Payload(alert="A message from " +self.userName+ " posted to "+self.channelName, sound="default", badge=1, custom={"channel":self.channelID})

        if len(self.retryAPNList)>0:
            token +=1
            for x in self.retryAPNList:
                self.apns.gateway_server.send_notification(x, payload, identifier = token)
            time.sleep(0.5)

分辨率:

如底部评论中所述,解决此问题的方法是将以下语句移至类外的模块级别.这样做就不需要任何 sleep 语句.

As noted in the comments at bottom, the resolution to this problem was to move the following statement to the module level outside the class. By doing this there is no need for any sleep statements.

apns = APNs(use_sandbox=True,cert_file="mycert.pem", key_file="mykey.pem", enhanced=True)

推荐答案

事实上,PyAPNS 会自动为你重新发送丢弃的通知,请参阅 PyAPNS

In fact, PyAPNS will auto resend dropped notifications for you, please see PyAPNS

所以你不必自己重试,你可以只记录哪些通知有错误的令牌.

So you don't have to retry by yourself, you can just record what notifications have bad tokens.

您的代码的行为可能是由于 APNS 对象保存在本地范围内(在 if len(self.retryAPNList)>0: 内)

The behavior of your code might be result from APNS object kept in local scope (within if len(self.retryAPNList)>0:)

建议您将APNS对象拉出到类或模块级别,这样它就可以完成其错误处理过程并重用TCP连接.

I suggest you to pull out APNS object to class or module level, so that it can complete its error handling procedure and reuse the TCP connection.

如果有帮助,请告诉我,谢谢:)

Please kindly let me know if it helps, thanks :)

这篇关于PyAPN 和在发送之间休眠的需要的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆