jazzband / django-push-notifications

Send push notifications to mobile devices through GCM or APNS in Django.
MIT License
2.27k stars 614 forks source link

Sending a message to APNS Device blocks the server thread #124

Open YuriHeupa opened 9 years ago

YuriHeupa commented 9 years ago

When sending a Push Notification to apple devices the server blocks connections for a while and it dont release until the message is sent, is this the expected behavior?

jleclanche commented 9 years ago

Well, yes and no - it's not threaded, so it's expected but it sure isn't what we want. I'm open to PRs.

evdoks commented 9 years ago

We solved this problem by creating a celery tasks that wrap apns_send_bulk_message() and gcm_send_bulk_message() so that the sending could happen asynchronously. Also scales nicely for larger numbers of devices.

YuriHeupa commented 9 years ago

@evdoks To use celery i need to change inside the apns and gcm code, can you show me which decorator of celery api you used?

evdoks commented 9 years ago

We did not change anything inside django-push-notifications. What we did is we created an asynchronous wrapper around synchronous APNSDeviceQuerySet.send_message() and GCMDeviceQuerySet.send_message(). Here is a simplified representation of this idea

@app.task(ignore_result=True)
def send_ios_notifications(ios_devices):
    ios_devices.send_message()

@app.task(ignore_result=True)
def send_android_notifications(android_devices):
    android_devices.send_messages()

Now you can call these functions asynchronously:

send_ios_notifications.delay(ios_devices)
send_android_notifications.delay(android_devices)
koutoftimer commented 9 years ago

@jleclanche Are you sure about async execution? I mean there are several types that allow such things. It can be multithreading (inefficient for cpython because of GIL), multiprocessing and concurrent execution. As for me, best of it is concurrency, because most delays related io operations.

For python 2 solution can be gevents, but relying on personal experience it is not good choice. Actually, I have not found reliable solution for second version of python.

For python 3 solution can be integrated event loop for managing concurrency.

Any way, even if we implement async execution in some way, how to handle:

@evdoks Would you like to add this recipe in README.md with titile "How to"?

codinalot commented 8 years ago

Hello all; was this ever taken care of? I'm wondering if my server thread is blocking upon sending a notification..