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

arminrock
  • 525
  • 1
  • 7
  • 23

2 Answers2

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.

jaap3
  • 2,696
  • 19
  • 34
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.

RemcoGerlich
  • 30,470
  • 6
  • 61
  • 79
  • Note that Channels [will not be](https://groups.google.com/forum/#!topic/django-developers/QRd4v7OErT8) in 1.10, but will live as a separate app for the foreseeable future. That shouldn't stop you from using it, though! – knbk Jul 07 '16 at 08:43
  • After the answers i started reading and learning the concept and implementation of ``channles``. Thanks for the feedbacks – arminrock Jul 07 '16 at 09:15