Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sending "async" status update and final result #728

Closed
sam-thecoder opened this issue Aug 28, 2017 · 1 comment
Closed

sending "async" status update and final result #728

sam-thecoder opened this issue Aug 28, 2017 · 1 comment

Comments

@sam-thecoder
Copy link

sam-thecoder commented Aug 28, 2017

I have a problem wrapping my mind on the best way to implement this; I have this code in the consumers.py

import json
from channels import Group
from channels.sessions import channel_session

# Connected to websocket.connect
@channel_session
def ws_connect(message, key):
    # Accept connection
    message.reply_channel.send({"accept": True})
    # Parse the query string
    print message

    #Get key and set using key
    message.channel_session["key"] = key
    
    #Set with key
    Group("p-%s"%key).add(message.reply_channel)
    
    #else:
        # Close the connection.
        #message.reply_channel.send({"close": True})

# Connected to websocket.receive
@channel_session
def ws_message(message, key):
    reply = {}
    json_response = json.dumps(reply)
    Group("p-%s"%key).send({
        "text": json_response,
    })

# Connected to websocket.disconnect
@channel_session
def ws_disconnect(message, key):
    Group("p-%s"%key).discard(message.reply_channel)

and I could either move it or import it in my program flow is for an image processing which should return the status of the process and final result.

for _ in range(x):
    #insert code for ws_connect
    ...
    if counter%100 == 0:
        #send update (no further data is needed from user so it would be a ws_send like function)

#send proccessed result (ws_send)
#ws_disconnect

honestly this is far easier than celery and I hope you can help me finish setting it up.
also there's a part in you're docs where you have an import that doesn't work in python 2.7

In [1]: from urllib.parse import parse_qs
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-1-884805ae250f> in <module>()
----> 1 from urllib.parse import parse_qs

ImportError: No module named parse
@andrewgodwin
Copy link
Member

The import is for Python 3, which is what the docs are written for - you'll have to adapt it for Python 2.

I'll answer the more open-ended question on the mailing list, since this is not the right place.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants