3

I'm new to python and Django and i'm trying to implement websockets in Django.
What i do is i'm following the steps described in websockets documentation
The problem is that the server side command described has to be run in console. When i run it from console it works, but i want to run it inside the Django view asynchronously with a GET request. When i try it the server raises an exception something like this RuntimeError: There is no current event loop in thread 'Thread-2'.
To be more specific I want to use the technology to show a real time logs. For example an oracle procedure performs an insert and server pushes it to a page with websockets.
Am I on a wrong path for implementing the described or can anyone suggest a right/better solution?

I'm on django version 1.9 implemented on both Django's development server and Uwsgi and Nginx server, python version 3.5.2 on RedHatEnterpriseServer Release: 6.7


UPDATE
The exact code from the above URL and i put it in the view.

def ws(request):

    async def time(websocket, path):
        while True:
            now = datetime.datetime.utcnow().isoformat() + 'Z'
            await websocket.send(now)
            await asyncio.sleep(random.random() * 3)

    start_server = websockets.serve(time, '192.168.4.177', 9876)

    asyncio.get_event_loop().run_until_complete(start_server)
    asyncio.get_event_loop().run_forever()

return render(request,"ws.html")

When the URL is handled by this view the above mentioned error occurs.

My ws.html is the exact copy from the above mentioned websockets documentation example

2
  • Could you please show us what you tried? Commented Jul 7, 2016 at 7:36
  • @juankysmith Please see the update. Commented Jul 7, 2016 at 7:48

2 Answers 2

1

Django's request/response cycle is strictly synchronous. What you are trying to do is not possible in a normal Django view.

You might be interested in Django Channels, a project that aims to remove this limitation.

Sign up to request clarification or add additional context in comments.

Comments

1

You can't really do this. I can't say why you're getting the exact errors you're getting, but a GET request to a Django view needs to return a response after some finite time, not run forever, otherwise the browser (or other parts in between like Nginx) will see the non-response as a timeout. If you want to run a websocket server, do it in a separate process outside of Django's.

There is much ongoing work to add async functionality and websockets to Django, in the form of channels -- I think the docs at http://channels.readthedocs.io/en/latest/ are the latest version of the code you can currently already use; hopefully it will be part of Django 1.10. Current version should be useable as a Django app that will allow you to make websockets in Django, but it's not as easy as your try above.

2 Comments

Note that Channels will not be in 1.10, but will live as a separate app for the foreseeable future. That shouldn't stop you from using it, though!
After the answers i started reading and learning the concept and implementation of channles. Thanks for the feedbacks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.