Re: Async Help Needed

2021-08-12 Thread Andrew Godwin
The example you presented should Just Work - that error generally only occurs 
if you return an un-awaited coroutine, which if you're just returning a 
response, should be impossible.

What version of Python and Django are you running?

Andrew

On Thu, Aug 12, 2021, at 11:03 AM, Steven Mapes wrote:
> Does anyone have some links to good guides on getting asnyc to work with 
> Django? I have a use case where I really need to have a view give an async 
> response but I can't get even the most basic of examples working.
> 
> I've tried the examples from https://testdriven.io/blog/django-async-views/ 
> with a simple view of:
> 
> async def test_view(request):
> return HttpResponse("Hello, async Django!")
> 
> I've installed uvicorn and am running it with  uvicorn 
> my_project.asgi:application --reload
> 
> My sync views work as expected but the async one just returns an error of 
> "*The view sample.views.test_view didn't return an HttpResponse object. It 
> returned an unawaited coroutine instead. You may need to add an 'await' into 
> your view.*"
> 
> 
> 
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to django-users+unsubscr...@googlegroups.com.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/django-users/64f90a01-76fe-4e01-ab3a-a6a0910038c9n%40googlegroups.com
>  
> .

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/378098bf-4754-4d4e-afc0-82eb04873438%40www.fastmail.com.


Re: register_converter doesn't work with ASGI

2021-06-20 Thread Andrew Godwin
Ah yes, that's because the URL parsing/conversion all happens in the main 
(async, in ASGI mode) thread - and because to_python is a a synchronous 
function interface, there is literally nothing you can do to make it work.

The only real way of fixing this would be for Django to patch the URL 
converters and run them all in a synchronous mode unless they were decorated as 
"safe" somehow, much like we do for middleware. It's not a terribly hard patch 
to make, but it does mean you don't have an immediate solution. There are ways 
of trying to work around it, but they will all result in you blocking the async 
thread while the ORM query runs, which would be disastrous in production.

My apologies for not getting this in for the original async view patch - I had 
forgotten converters ran in the URL resolver.

Andrew

On Sat, Jun 19, 2021, at 12:31 AM, konstanti...@gmail.com 
 wrote:
> 
> I'm trying to switch from WSGI to ASGI and I'm having issues with my model id 
> to model converters.
> I define my converters like this
> def to_python(self, primary_key):
>  try:
>   return self.model.objects.get(pk=primary_key)
>  except self.model.DoesNotExist:
>   raise ValueError
> When I go to a url where a converter is used I get a SynchronousOnlyOperation.
> Seems like converters are executed in an async context.
> If I use sync_to_async, I get a coroutine in my view instead of a model 
> instance. Awaiting it here would defeat the purpose of having a converter.
> 
> Any ideas how to fix this?
> 

> -- 
> You received this message because you are subscribed to the Google Groups 
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to django-users+unsubscr...@googlegroups.com.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/django-users/ef60ed71-7f4f-482d-b540-bdf89e249aa4n%40googlegroups.com
>  
> .

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/34c22d74-ba14-45b0-81fa-85f03c5f34af%40www.fastmail.com.


Re: Daphne raises Internal Server Error when accessing static files from development server

2021-04-06 Thread Andrew Godwin
This is probably this error: 
https://github.com/django/asgiref/issues/251#issuecomment-814337170 - I'm going 
to switch the error to a warning and we'll see if that helps, then Daphne can 
have a fix added at a non-emergency pace.

Andrew

On Tue, Apr 6, 2021, at 12:19 PM, kradem wrote:
> Not sure if I understand you, I'm getting an server error in my browser with 
> rendered as quoted in the first post here with the following body:
> 
>500 Internal Server Error   body { 
> font-family: sans-serif; margin: 0; padding: 0; }  h1 { padding: 0.6em 0 
> 0.2em 20px; color: #896868; margin: 0; }  p { padding: 0 0 0.3em 20px; 
> margin: 0; }  footer { padding: 1em 0 0.3em 20px; color: #999; font-size: 
> 80%; font-style: italic; }500 Internal Server 
> Error Exception inside application. Daphne 
>   
> And as raw:
> 
> HTTP/1.1 500 Internal Server Error
> Transfer-Encoding: chunked
> Content-Type: text/html; charset=utf-8
> 
> Dana utorak, 6. travnja 2021. u 18:26:34 UTC+2 korisnik Andrew Godwin napisao 
> je:
>> __
>> The tracebacks are there somewhere, you might have to look at the raw HTTP 
>> response in the browser to see them.
>> 
>> Andrew
>> 
>> On Tue, Apr 6, 2021, at 10:18 AM, kradem wrote:
>>> I'm afraid a "success" was caused by the browser cache or something 
>>> similar, the 3.3.3 version doesn't fix my problem. :(
>>> 
>>> > Could you get the traceback out of Django that's causing the 500 error 
>>> > and post it here? 
>>> 
>>> That was - kind of - my initial question, the only output I can get is the 
>>> message from the first post and Django's output like:
>>> 
>>> HTTP GET /static/css/style.css 500 [0.35, 127.0.0.1:37982]
>>> HTTP GET /static/js/jquery-2.2.4.min.js 500 [0.35, 127.0.0.1:37988]
>>> 
>>> Dana utorak, 6. travnja 2021. u 18:09:33 UTC+2 korisnik Andrew Godwin 
>>> napisao je:
>>>> __
>>>> Aha, glad to hear it! My apologies that we didn't catch that one before 
>>>> release yesterday.
>>>> 
>>>> Andrew
>>>> 
>>>> On Tue, Apr 6, 2021, at 10:01 AM, kradem wrote:
>>>>> Stop the press!! The 3.3.3 version is published in PyPi and after 
>>>>> installing it it all works well! :)
>>>>> 
>>>>> Thank you!
>>>>> Dana utorak, 6. travnja 2021. u 17:56:44 UTC+2 korisnik kradem napisao je:
>>>>>> Thank you very much for a quick response.
>>>>>> 
>>>>>> Yes, that's the problem:
>>>>>> 
>>>>>> asgiref   3.3.2
>>>>>> 
>>>>>> When I downgrade it to 3.3.1 Django complains ("django 3.2 requires 
>>>>>> asgiref<4,>=3.3.2, but you have asgiref 3.3.1 which is incompatible."), 
>>>>>> but static files are served well.
>>>>>> 
>>>>>> I tried with the very last version 3.3.3 (-e 
>>>>>> git+https://github.com/django/asgiref.git#egg=asgiref), but problem 
>>>>>> hasn't been fixed that way.
>>>>>> 
>>>>>> 
>>>>>> Dana utorak, 6. travnja 2021. u 17:46:31 UTC+2 korisnik Andrew Godwin 
>>>>>> napisao je:
>>>>>>> __
>>>>>>> This may be related to https://github.com/django/asgiref/issues/251 - 
>>>>>>> what version of asgiref are you running?
>>>>>>> 
>>>>>>> Andrew
>>>>>>> 
>>>>>>> On Tue, Apr 6, 2021, at 9:43 AM, kradem wrote:
>>>>>>>> > 500 Internal Server Error 
>>>>>>>> > Exception inside application.

>>>>>>>> > Daphne
>>>>>>>> 
>>>>>>>> This error raises every time a file from static folder is accessed.
>>>>>>>> 
>>>>>>>> What would be a proper way for debugging this? I've got  "justMyCode": 
>>>>>>>> false setting in VS Code and that doesn't catch anything.
>>>>>>>> 
>>>>>>>> Python 3.9.2
>>>>>>>> 
>>>>>>>> channels  3.0.3
>>>>>>>> daphne3.0.1
>>>>>>>> Django3.2
>>>>>>>> 
>>>>>>>> 

>>>>>>>> -- 
>>>>>>>> You received thi

Re: Daphne raises Internal Server Error when accessing static files from development server

2021-04-06 Thread Andrew Godwin
The tracebacks are there somewhere, you might have to look at the raw HTTP 
response in the browser to see them.

Andrew

On Tue, Apr 6, 2021, at 10:18 AM, kradem wrote:
> I'm afraid a "success" was caused by the browser cache or something similar, 
> the 3.3.3 version doesn't fix my problem. :(
> 
> > Could you get the traceback out of Django that's causing the 500 error and 
> > post it here? 
> 
> That was - kind of - my initial question, the only output I can get is the 
> message from the first post and Django's output like:
> 
> HTTP GET /static/css/style.css 500 [0.35, 127.0.0.1:37982]
> HTTP GET /static/js/jquery-2.2.4.min.js 500 [0.35, 127.0.0.1:37988]
> 
> Dana utorak, 6. travnja 2021. u 18:09:33 UTC+2 korisnik Andrew Godwin napisao 
> je:
>> __
>> Aha, glad to hear it! My apologies that we didn't catch that one before 
>> release yesterday.
>> 
>> Andrew
>> 
>> On Tue, Apr 6, 2021, at 10:01 AM, kradem wrote:
>>> Stop the press!! The 3.3.3 version is published in PyPi and after 
>>> installing it it all works well! :)
>>> 
>>> Thank you!
>>> Dana utorak, 6. travnja 2021. u 17:56:44 UTC+2 korisnik kradem napisao je:
>>>> Thank you very much for a quick response.
>>>> 
>>>> Yes, that's the problem:
>>>> 
>>>> asgiref   3.3.2
>>>> 
>>>> When I downgrade it to 3.3.1 Django complains ("django 3.2 requires 
>>>> asgiref<4,>=3.3.2, but you have asgiref 3.3.1 which is incompatible."), 
>>>> but static files are served well.
>>>> 
>>>> I tried with the very last version 3.3.3 (-e 
>>>> git+https://github.com/django/asgiref.git#egg=asgiref), but problem hasn't 
>>>> been fixed that way.
>>>> 
>>>> 
>>>> Dana utorak, 6. travnja 2021. u 17:46:31 UTC+2 korisnik Andrew Godwin 
>>>> napisao je:
>>>>> __
>>>>> This may be related to https://github.com/django/asgiref/issues/251 - 
>>>>> what version of asgiref are you running?
>>>>> 
>>>>> Andrew
>>>>> 
>>>>> On Tue, Apr 6, 2021, at 9:43 AM, kradem wrote:
>>>>>> > 500 Internal Server Error 
>>>>>> > Exception inside application.

>>>>>> > Daphne
>>>>>> 
>>>>>> This error raises every time a file from static folder is accessed.
>>>>>> 
>>>>>> What would be a proper way for debugging this? I've got  "justMyCode": 
>>>>>> false setting in VS Code and that doesn't catch anything.
>>>>>> 
>>>>>> Python 3.9.2
>>>>>> 
>>>>>> channels  3.0.3
>>>>>> daphne3.0.1
>>>>>> Django3.2
>>>>>> 
>>>>>> 

>>>>>> -- 
>>>>>> You received this message because you are subscribed to the Google 
>>>>>> Groups "Django users" group.
>>>>>> To unsubscribe from this group and stop receiving emails from it, send 
>>>>>> an email to django-users...@googlegroups.com.
>>>>>> To view this discussion on the web visit 
>>>>>> https://groups.google.com/d/msgid/django-users/9db2fc19-e87c-4a69-ab9e-6c53ec56d6fcn%40googlegroups.com
>>>>>>  
>>>>>> <https://groups.google.com/d/msgid/django-users/9db2fc19-e87c-4a69-ab9e-6c53ec56d6fcn%40googlegroups.com?utm_medium=email_source=footer>.
>>>>> 
>>> 

>>> -- 
>>> You received this message because you are subscribed to the Google Groups 
>>> "Django users" group.
>>> To unsubscribe from this group and stop receiving emails from it, send an 
>>> email to django-users...@googlegroups.com.
>>> To view this discussion on the web visit 
>>> https://groups.google.com/d/msgid/django-users/bd27e80e-de40-4dfd-b215-7855f537b191n%40googlegroups.com
>>>  
>>> <https://groups.google.com/d/msgid/django-users/bd27e80e-de40-4dfd-b215-7855f537b191n%40googlegroups.com?utm_medium=email_source=footer>.
> 

> -- 
> You received this message because you are subscribed to the Google Groups 
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to django-users+unsubscr...@googlegroups.com.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/django-users/7a5a269b-9099-4bbf-af72-65b6c77bb511n%40googlegroups.com
>  
> <https://groups.google.com/d/msgid/django-users/7a5a269b-9099-4bbf-af72-65b6c77bb511n%40googlegroups.com?utm_medium=email_source=footer>.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/789cfaea-899b-4f23-885b-4e2857e7dbd1%40www.fastmail.com.


Re: Daphne raises Internal Server Error when accessing static files from development server

2021-04-06 Thread Andrew Godwin
Aha, glad to hear it! My apologies that we didn't catch that one before release 
yesterday.

Andrew

On Tue, Apr 6, 2021, at 10:01 AM, kradem wrote:
> Stop the press!! The 3.3.3 version is published in PyPi and after installing 
> it it all works well! :)
> 
> Thank you!
> Dana utorak, 6. travnja 2021. u 17:56:44 UTC+2 korisnik kradem napisao je:
>> Thank you very much for a quick response.
>> 
>> Yes, that's the problem:
>> 
>> asgiref   3.3.2
>> 
>> When I downgrade it to 3.3.1 Django complains ("django 3.2 requires 
>> asgiref<4,>=3.3.2, but you have asgiref 3.3.1 which is incompatible."), but 
>> static files are served well.
>> 
>> I tried with the very last version 3.3.3 (-e 
>> git+https://github.com/django/asgiref.git#egg=asgiref), but problem hasn't 
>> been fixed that way.
>> 
>> 
>> Dana utorak, 6. travnja 2021. u 17:46:31 UTC+2 korisnik Andrew Godwin 
>> napisao je:
>>> __
>>> This may be related to https://github.com/django/asgiref/issues/251 - what 
>>> version of asgiref are you running?
>>> 
>>> Andrew
>>> 
>>> On Tue, Apr 6, 2021, at 9:43 AM, kradem wrote:
>>>> > 500 Internal Server Error 
>>>> > Exception inside application.

>>>> > Daphne
>>>> 
>>>> This error raises every time a file from static folder is accessed.
>>>> 
>>>> What would be a proper way for debugging this? I've got  "justMyCode": 
>>>> false setting in VS Code and that doesn't catch anything.
>>>> 
>>>> Python 3.9.2
>>>> 
>>>> channels  3.0.3
>>>> daphne3.0.1
>>>> Django3.2
>>>> 
>>>> 

>>>> -- 
>>>> You received this message because you are subscribed to the Google Groups 
>>>> "Django users" group.
>>>> To unsubscribe from this group and stop receiving emails from it, send an 
>>>> email to django-users...@googlegroups.com.
>>>> To view this discussion on the web visit 
>>>> https://groups.google.com/d/msgid/django-users/9db2fc19-e87c-4a69-ab9e-6c53ec56d6fcn%40googlegroups.com
>>>>  
>>>> <https://groups.google.com/d/msgid/django-users/9db2fc19-e87c-4a69-ab9e-6c53ec56d6fcn%40googlegroups.com?utm_medium=email_source=footer>.
>>> 
> 

> -- 
> You received this message because you are subscribed to the Google Groups 
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to django-users+unsubscr...@googlegroups.com.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/django-users/bd27e80e-de40-4dfd-b215-7855f537b191n%40googlegroups.com
>  
> <https://groups.google.com/d/msgid/django-users/bd27e80e-de40-4dfd-b215-7855f537b191n%40googlegroups.com?utm_medium=email_source=footer>.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/8c9616ad-021a-4162-8f52-b0be27f46d94%40www.fastmail.com.


Re: Daphne raises Internal Server Error when accessing static files from development server

2021-04-06 Thread Andrew Godwin
asgiref 3.3.2 introduced stricter checks for what an async function is, so I 
would imagine that's the problem. 3.3.3 fixes some of them, but I'm guessing 
you might have a different flavour of problem.

Could you get the traceback out of Django that's causing the 500 error and post 
it here? 

Andrew

On Tue, Apr 6, 2021, at 9:56 AM, kradem wrote:
> Thank you very much for a quick response.
> 
> Yes, that's the problem:
> 
> asgiref   3.3.2
> 
> When I downgrade it to 3.3.1 Django complains ("django 3.2 requires 
> asgiref<4,>=3.3.2, but you have asgiref 3.3.1 which is incompatible."), but 
> static files are served well.
> 
> I tried with the very last version 3.3.3 (-e 
> git+https://github.com/django/asgiref.git#egg=asgiref), but problem hasn't 
> been fixed that way.
> 
> 
> Dana utorak, 6. travnja 2021. u 17:46:31 UTC+2 korisnik Andrew Godwin napisao 
> je:
>> __
>> This may be related to https://github.com/django/asgiref/issues/251 - what 
>> version of asgiref are you running?
>> 
>> Andrew
>> 
>> On Tue, Apr 6, 2021, at 9:43 AM, kradem wrote:
>>> > 500 Internal Server Error 
>>> > Exception inside application.

>>> > Daphne
>>> 
>>> This error raises every time a file from static folder is accessed.
>>> 
>>> What would be a proper way for debugging this? I've got  "justMyCode": 
>>> false setting in VS Code and that doesn't catch anything.
>>> 
>>> Python 3.9.2
>>> 
>>> channels  3.0.3
>>> daphne3.0.1
>>> Django3.2
>>> 
>>> 

>>> -- 
>>> You received this message because you are subscribed to the Google Groups 
>>> "Django users" group.
>>> To unsubscribe from this group and stop receiving emails from it, send an 
>>> email to django-users...@googlegroups.com.
>>> To view this discussion on the web visit 
>>> https://groups.google.com/d/msgid/django-users/9db2fc19-e87c-4a69-ab9e-6c53ec56d6fcn%40googlegroups.com
>>>  
>>> <https://groups.google.com/d/msgid/django-users/9db2fc19-e87c-4a69-ab9e-6c53ec56d6fcn%40googlegroups.com?utm_medium=email_source=footer>.
>> 
> 

> -- 
> You received this message because you are subscribed to the Google Groups 
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to django-users+unsubscr...@googlegroups.com.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/django-users/5c9ffb1b-c644-41c6-a5c3-a52bb95a1893n%40googlegroups.com
>  
> <https://groups.google.com/d/msgid/django-users/5c9ffb1b-c644-41c6-a5c3-a52bb95a1893n%40googlegroups.com?utm_medium=email_source=footer>.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/04bc66bc-1cfc-4c02-8333-45b12af186e8%40www.fastmail.com.


Re: Daphne raises Internal Server Error when accessing static files from development server

2021-04-06 Thread Andrew Godwin
This may be related to https://github.com/django/asgiref/issues/251 - what 
version of asgiref are you running?

Andrew

On Tue, Apr 6, 2021, at 9:43 AM, kradem wrote:
> > 500 Internal Server Error 
> > Exception inside application.

> > Daphne
> 
> This error raises every time a file from static folder is accessed.
> 
> What would be a proper way for debugging this? I've got  "justMyCode": false 
> setting in VS Code and that doesn't catch anything.
> 
> Python 3.9.2
> 
> channels  3.0.3
> daphne3.0.1
> Django3.2
> 
> 

> -- 
> You received this message because you are subscribed to the Google Groups 
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to django-users+unsubscr...@googlegroups.com.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/django-users/9db2fc19-e87c-4a69-ab9e-6c53ec56d6fcn%40googlegroups.com
>  
> .

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/5db33183-8f45-43b9-9485-9e909bea5058%40www.fastmail.com.


Re: Channels, daphne, and asgiref, oh my!

2020-02-21 Thread Andrew Godwin
Hi Larry,

Your problem is the Python version - 3.5 is somewhat old (but most
importantly, was still a very rough release for asyncio) and isn't
supported by any current release of asgiref/channels for that reason. If
you are able to upgrade that, I very much recommend it.

Andrew

On Sat, Feb 22, 2020 at 2:52 AM Aldian Fazrihady  wrote:

> I have these working module combinations, but my Python is 3.6.9:
> asgiref==2.2.0
> channels==2.0.2
> channels_redis==2.3.0
> daphne==2.1.0
> Django==2.2.4
> django-redis==4.10.0
> redis==3.3.6
> Twisted[tls,http2]
> uwsgi==2.0.18
> websockets==6.0
>
> On Sat, Feb 22, 2020 at 3:25 AM Larry Martell 
> wrote:
>
>> I am in a bit of version hell. Not fun on a Friday afternoon.
>>
>> Django 2.0.4, python 3.5.2
>>
>> Trying to get channels, websockets, daphne, and asgiref working.
>>
>> When I installed the latest channels it did not work with my version
>> of mysqlclient and some googling led me to install 2.0 of channels.
>> That worked fine with the dev server, and then I wanted to deploy my
>> app in prod. In prod we use nginx/uwsgi so I was going to replace
>> uwsgi with daphne. When I tried to run daphne I got:
>>
>> ImportError: No module named 'asgiref.compatibility'
>>
>> Googling that I was led to upgrade asgiref to the latest version.
>> After I did that I got:
>>
>> AttributeError: module 'asyncio.coroutines' has no attribute
>> '_is_coroutine'
>>
>> Have not been able to get past this one.
>>
>> Anyone have any thoughts? Would upgrading django and python fix these
>> issues? I am planning on doing that soon, but I was not planning on
>> doing that now. Don't want to upgrade now and then find I still have
>> issues. Can I get all this to work with my current versions and
>> upgrade later?
>>
>> TIA!
>> Larry
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Django users" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to django-users+unsubscr...@googlegroups.com.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/django-users/CACwCsY5SQzRG-7EuEM8rZvOfQor5bBOjt%3DBV1Qsbn9%2BBhMx12g%40mail.gmail.com
>> .
>>
>
>
> --
> Regards,
>
> Aldian Fazrihady
> http://aldianfazrihady.com
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAN7EoAbSAVUT%3D8sg1o-0oczCfjfRhtGwrMDE31YBt9N%2Bm96YmA%40mail.gmail.com
> 
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAFwN1up5WAkwAVrasG%2BgG6ia8JSru1sMjJnckiCP8kbsVK-JiA%40mail.gmail.com.


Re: Channels 2 using asyncio run_in_executor

2019-10-18 Thread Andrew Godwin
It should be fine, but you will need to be careful - there's a lot of
side-effects with run_in_executor you should be aware of like the way
exceptions propagate (Django won't handle them right for you) and the
possibility of deadlock.

Andrew

On Fri, Oct 18, 2019 at 11:27 AM BR  wrote:

> Should I expect any issues using asyncio run_in_executor to run blocking
> code? I realize there is the sync_to_async function as well, but that
> wasn't working in my case. I'm using multiprocessing.Queue to share data
> with a new process, and when I try to use queue.get() with either
> sync_to_async or run_in_executor, the calling method hangs.
>
> My solution for now was to run a queue.get() listener in a separate
> thread, but it would have been nicer to use await with run_in_executor. Is
> there any custom configuration of the consumer event loop that prevents
> some times of functions from running, even when run with appropriate
> wrapping functions?
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/3be88116-f199-483e-9ee9-73d3a292c81d%40googlegroups.com
> 
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAFwN1uo%2BC9wS-%2BjEpz9OaUXcS7hO2SauxA9evUUuTiraOS69ww%40mail.gmail.com.


Re: Channels: launching a custom co-routine from SyncConsumer

2019-08-15 Thread Andrew Godwin
SyncConsumer isn't async - it runs inside a separate synchronous thread.
You'll need to get the event loop from the other thread and use
call_soon_threadsafe instead!

Andrew

On Wed, Aug 14, 2019 at 6:59 PM Dan Merillat  wrote:

>
> I have an application that's 99% using the ORM and database work so it is
> using SyncConsumer.
> However, I have one minor part that needs to use a timer, and I cannot get
> it to work for the life of me.
>
> In an AsyncConsumer, I can use asyncio.ensure_future(self.coroutine()) to
> start a timer.
>
> In sync consumer, nothing I've tried works:
>
> class TestConsumer(SyncConsumer):
>
> async def timer_task(self):
> print(f'timer task: {self.counter}')
> asyncio.sleep(1)
> self.counter += 1
> print(f'timer expired: {self.counter}')
>
> def connect(self):
> self.counter = 0
>
> self.timer = asyncio.ensure_future(self.timer_task())
> # ERROR:daphne.server:Exception inside application: There is no
> current event loop in thread 'ThreadPoolExecutor-0_20'.
>
> self.timer =
> async_to_sync(asyncio.ensure_future)(self.timer_task())
> # ERROR:daphne.server:Exception inside application: You cannot use
> AsyncToSync in the same thread as an async event loop - just await the
> async function directly.
>
> I thought SyncConsumer was async under the hood, but I have no idea how to
> leverage that.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/6fe7ad95-1bdb-4de2-9ed2-8c566ba27939%40googlegroups.com
> 
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAFwN1uoGn%2B7C0hLFat%3DjMVgOKL2nYHAuae1i6mrC-JRW6W0uww%40mail.gmail.com.


Re: Getting AttributeError: module 'asyncio' has no attribute '_get_running_loop'

2019-07-08 Thread Andrew Godwin
The bug was only on Python 3.5. It's possible the other system was 3.6 or
3.7?

Andrew

On Sun, Jul 7, 2019 at 9:48 AM Rohit Chopra  wrote:

> Hi Andrew,
>
> Just a small doubt, why same code is working on my local system.
> Both local and server have same  version of requirements mentioned in
> requirements.txt
>
> Rohit
>
> On Sun 7 Jul, 2019, 9:36 PM Andrew Godwin,  wrote:
>
>> Hi Rohit,
>>
>> This is my fault - we made a change in the "asgiref" library that
>> inadvertently removed Python 3.5 support. Channels still needs to support
>> it (even though Django doesn't).
>>
>> If you update to asgiref 3.1.4, which I've just released, that should fix
>> the issue.
>>
>> Andrew
>>
>> On Sun, Jul 7, 2019 at 8:52 AM Rohit Chopra 
>> wrote:
>>
>>> Hi All,
>>>
>>> I am using channels to implement WebSockets.
>>> I am trying to send data through WebSockets from *post_save *django
>>> signal.
>>> Below is my code.
>>>
>>> **signals.py**
>>>
>>>
>>> @receiver(post_save, sender=CheckIn)
>>> def send_data_on_save(sender, instance, **kwargs):
>>> channel_layer = get_channel_layer()
>>> stats = get_stats()
>>> async_to_sync(channel_layer.group_send)(
>>> 'dashboard',
>>> {
>>> 'type': 'send_data',
>>> 'message': stats
>>> }
>>> )
>>>
>>>
>>> analytics = get_analytics()
>>> async_to_sync(channel_layer.group_send)(
>>> 'analytic',
>>> {
>>> 'type': 'send_data',
>>> 'message': analytics
>>> }
>>> )
>>>
>>>
>>>
>>> **consumers.py**
>>>
>>>
>>> class DashboardConsumer(WebsocketConsumer):
>>> def connect(self):
>>> self.room_group_name = 'dashboard'
>>>
>>>
>>> # Join room group
>>> async_to_sync(self.channel_layer.group_add)(
>>> self.room_group_name,
>>> self.channel_name
>>> )
>>>
>>>
>>> self.accept()
>>>
>>>
>>> def disconnect(self, close_code):
>>> # Leave room group
>>> async_to_sync(self.channel_layer.group_discard)(
>>> self.room_group_name,
>>> self.channel_name
>>> )
>>>
>>>
>>> # Receive message from WebSocket
>>> def receive(self, text_data):
>>> text_data_json = json.loads(text_data)
>>> message = text_data_json['message']
>>>
>>>
>>> # Send message to room group
>>> async_to_sync(self.channel_layer.group_send)(
>>> self.room_group_name,
>>> {
>>> 'type': 'send_data',
>>> 'message': message
>>> }
>>> )
>>>
>>>
>>> # Receive message from room group
>>> def send_data(self, event):
>>> message = event['message']
>>>
>>>
>>> # Send message to WebSocket
>>> self.send(text_data=json.dumps({
>>> 'message': message
>>> }))
>>>
>>>
>>>
>>> this same piece of code is working on my local machine(windows) but when
>>> i am trying to run this code on server(ubuntu 16.04) i am getting bellow
>>> error:
>>>
>>> **Traceback**
>>>
>>>
>>>
>>> Exception inside application: module 'asyncio' has no attribute
>>> '_get_running_loop'
>>> File "/usr/lib/python3.5/asyncio/tasks.py", line 241, in _step
>>> result = coro.throw(exc)
>>> File
>>> "/home/user1/demowebapps/env/lib/python3.5/site-packages/channels/sessions.py"
>>> , line 183, in __call__
>>> return await self.inner(receive, self.send)
>>> File
>>> "/home/user1/demowebapps/env/lib/python3.5/site-packages/channels/middleware.py"
>>> , line 41, in coroutine_call
>>> await inner_instance(receive, send)
>>> File
>>> "/home/user1/demowebapps/env/lib/python3.5/site-packages/channels/consumer.py"
>>> , line 59, in __call__
>>> [

Re: Getting AttributeError: module 'asyncio' has no attribute '_get_running_loop'

2019-07-07 Thread Andrew Godwin
Hi Rohit,

This is my fault - we made a change in the "asgiref" library that
inadvertently removed Python 3.5 support. Channels still needs to support
it (even though Django doesn't).

If you update to asgiref 3.1.4, which I've just released, that should fix
the issue.

Andrew

On Sun, Jul 7, 2019 at 8:52 AM Rohit Chopra  wrote:

> Hi All,
>
> I am using channels to implement WebSockets.
> I am trying to send data through WebSockets from *post_save *django
> signal.
> Below is my code.
>
> **signals.py**
>
>
> @receiver(post_save, sender=CheckIn)
> def send_data_on_save(sender, instance, **kwargs):
> channel_layer = get_channel_layer()
> stats = get_stats()
> async_to_sync(channel_layer.group_send)(
> 'dashboard',
> {
> 'type': 'send_data',
> 'message': stats
> }
> )
>
>
> analytics = get_analytics()
> async_to_sync(channel_layer.group_send)(
> 'analytic',
> {
> 'type': 'send_data',
> 'message': analytics
> }
> )
>
>
>
> **consumers.py**
>
>
> class DashboardConsumer(WebsocketConsumer):
> def connect(self):
> self.room_group_name = 'dashboard'
>
>
> # Join room group
> async_to_sync(self.channel_layer.group_add)(
> self.room_group_name,
> self.channel_name
> )
>
>
> self.accept()
>
>
> def disconnect(self, close_code):
> # Leave room group
> async_to_sync(self.channel_layer.group_discard)(
> self.room_group_name,
> self.channel_name
> )
>
>
> # Receive message from WebSocket
> def receive(self, text_data):
> text_data_json = json.loads(text_data)
> message = text_data_json['message']
>
>
> # Send message to room group
> async_to_sync(self.channel_layer.group_send)(
> self.room_group_name,
> {
> 'type': 'send_data',
> 'message': message
> }
> )
>
>
> # Receive message from room group
> def send_data(self, event):
> message = event['message']
>
>
> # Send message to WebSocket
> self.send(text_data=json.dumps({
> 'message': message
> }))
>
>
>
> this same piece of code is working on my local machine(windows) but when i
> am trying to run this code on server(ubuntu 16.04) i am getting bellow
> error:
>
> **Traceback**
>
>
>
> Exception inside application: module 'asyncio' has no attribute
> '_get_running_loop'
> File "/usr/lib/python3.5/asyncio/tasks.py", line 241, in _step
> result = coro.throw(exc)
> File
> "/home/user1/demowebapps/env/lib/python3.5/site-packages/channels/sessions.py"
> , line 183, in __call__
> return await self.inner(receive, self.send)
> File
> "/home/user1/demowebapps/env/lib/python3.5/site-packages/channels/middleware.py"
> , line 41, in coroutine_call
> await inner_instance(receive, send)
> File
> "/home/user1/demowebapps/env/lib/python3.5/site-packages/channels/consumer.py"
> , line 59, in __call__
> [receive, self.channel_receive], self.dispatch
> File
> "/home/user1/demowebapps/env/lib/python3.5/site-packages/channels/utils.py"
> , line 52, in await_many_dispatch
> await dispatch(result)
> File
> "/home/user1/demowebapps/env/lib/python3.5/site-packages/asgiref/sync.py",
> line 145, in __call__
> return await asyncio.wait_for(future, timeout=None)
> File "/usr/lib/python3.5/asyncio/tasks.py", line 373, in wait_for
> return (yield from fut)
> File "/usr/lib/python3.5/asyncio/futures.py", line 361, in __iter__
> yield self  # This tells Task to wait for completion.
> File "/usr/lib/python3.5/asyncio/tasks.py", line 296, in _wakeup
> future.result()
> File "/usr/lib/python3.5/asyncio/futures.py", line 274, in result
> raise self._exception
> File "/usr/lib/python3.5/concurrent/futures/thread.py", line 55, in
> run
> result = self.fn(*self.args, **self.kwargs)
> File
> "/home/user1/demowebapps/env/lib/python3.5/site-packages/channels/db.py",
> line 14, in thread_handler
> return super().thread_handler(loop, *args, **kwargs)
> File
> "/home/user1/demowebapps/env/lib/python3.5/site-packages/asgiref/sync.py",
> line 164, in thread_handler
> return func(*args, **kwargs)
> File
> "/home/user1/demowebapps/env/lib/python3.5/site-packages/channels/consumer.py"
> , line 105, in dispatch
> handler(message)
> File
> "/home/user1/demowebapps/env/lib/python3.5/site-packages/channels/generic/websocket.py"
> , line 39, in websocket_connect
> self.connect()
> File "/home/user1/demowebapps/vmsbackend/check_in/consumers.py", line
> 57, in connect
> self.channel_name
> File
> "/home/user1/demowebapps/env/lib/python3.5/site-packages/asgiref/sync.py",
> line 41, 

Re: Run Daphne as non-root on low ports

2019-05-02 Thread Andrew Godwin
In this case "reference server" merely refers to it being the place where
new features and specs are implemented and tested first, rather than being
the best production-capable one. I would say uvicorn is probably the best
ASGI server out there right now for performance.

Andrew

On Thu, 2 May 2019, 06:59 BR,  wrote:

> Thanks for the feedback. I guess I was thinking that since Daphne is the
> "reference server" for ASGI, that it would have features available for
> production type use. I only came across WebSocket stuff yesterday so this
> is all quite new. I suppose my remaining issue is that SSL certificates
> seem to only (usually) accessible by root, so in any situation it seems
> that Daphne can only terminate SSL when running in root, unless permissions
> on the private key are changed.
>
> I'm looking into the nginx reverse proxy with SSL termination at the
> moment.
>
> On Thursday, May 2, 2019 at 9:49:41 AM UTC-4, Andrew Godwin wrote:
>>
>> Daphne does not have uid-changing built in, no - it's too simplistic for
>> that, unfortunately.
>>
>> It's worth noting that systemd has this ability built in - it will listen
>> on sockets for you and bind the socket to an application as needed.
>> Otherwise, as the other poster noted, you can also use a reverse proxy to
>> terminate SSL, which is what Daphne is built assuming (the SSL support in
>> there is because it comes for free with Twisted, rather than being
>> specifically added).
>>
>> Andrew
>>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/c3a5b984-d625-4fc4-a792-22d48cb4d3a8%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/c3a5b984-d625-4fc4-a792-22d48cb4d3a8%40googlegroups.com?utm_medium=email_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAFwN1uqghNHtxUA1jGxFrOqtMfPyF_NqCFzgWac6BkXXj1U0VA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Run Daphne as non-root on low ports

2019-05-02 Thread Andrew Godwin
Daphne does not have uid-changing built in, no - it's too simplistic for
that, unfortunately.

It's worth noting that systemd has this ability built in - it will listen
on sockets for you and bind the socket to an application as needed.
Otherwise, as the other poster noted, you can also use a reverse proxy to
terminate SSL, which is what Daphne is built assuming (the SSL support in
there is because it comes for free with Twisted, rather than being
specifically added).

Andrew

On Thu, May 2, 2019 at 6:32 AM BR  wrote:

> I'm running a simple Django/Channels website using Daphne. Traffic will be
> relatively low and will likely only see 1-2 clients at a time. I figured
> this would be a good use case for Daphne to serve both HTTPS and WebSockets.
>
> Does Daphne have a mechanism for starting as root and dropping down to a
> non-root user after it is running? Root is required at startup because I
> want it to bind to port 80 or 443, and it needs to access the SSL
> certificate.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/1015c96c-1a20-4372-9de3-3ca6d2347d98%40googlegroups.com
> 
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAFwN1upBPizH8%2BSQdC9-qt-Z2SXgjtMqquKcMx5PJhswq2k8fg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: how handle high incoming data and sending throttled data to web sockets in django-channels

2019-01-14 Thread Andrew Godwin
You'll have to design your own consumption architecture if you want to
reliably handle 100 messages a second - since it's UDP, channels won't be
handling it natively so you'll need to terminate and process the UDP
yourself. I'd recommend either getting a UDP loadbalancer if you have
messages coming in from multiple sources, or do your own work queue if it's
a single source and you can't process them fast enough.

Andrew

On Mon, Jan 14, 2019 at 7:31 AM Sahil Mangotra <
sahil.mango...@utradesolutions.com> wrote:

> I am facing a problem in my django web server.
>
> We are using python3, django2, django-rest-framework3.8 and channels2.x
>
> Scenario is we are receiving DATA from a UDP connection at very fast rate
> (~100 messages per second). The data revived is in proto format (you can
> say we are receiving byte data). some data gets starved in this process as 
> *Rate
> of production >>> rate of consumption* we are implementing throttling but
> still at 100 concurrent users data starves again. Can anyone help us in
> this scenario.
>
> If anyone has any new architecture idea please share.
>
> *This is surely an interesting problem. This is about stock market feed*
>
> PS :- I cannot post any code as it is my companies. but i can help any
> time you need clarification on any point.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/02c1735d-dc05-44b3-b0a0-d343aa59f8ef%40googlegroups.com
> 
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAFwN1uqFZ%3DKe4yyPdG8-LTGRYLHYegotao9JbQNWOVWLJLoPcA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: channels and https

2018-12-28 Thread Andrew Godwin
You should use an ASGI server (uvicorn/daphne) with an SSL-terminating
server (e.g. nginx) in front of it and proxying to it. Django-sslserver is
a WSGI-based server and so can't support WebSockets.

Andrew

On Tue, Dec 25, 2018 at 4:44 PM  wrote:

> How can I  use channels with https at development
> I try to use django-sslserver but that doesn't work
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/1ecb3edd-3637-476d-8a61-5ac869f89daa%40googlegroups.com
> 
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAFwN1uqNWpnYop%3D%2BUL%2B86b1gUgUVzP0aiZ8LtqLoDu_YBB0BGQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Django channels communication not asynchronous

2018-12-28 Thread Andrew Godwin
Channels request/response cycles must be natively non-blocking all the way
from the application code up through the server. If you even so much as
slightly block you're going to disrupt everything and maybe deadlock it,
which is why you need to be careful about when you call synchronous things.

If you don't want to deal with all of this, use the SyncConsumer variants
of everything and/or normal django views which will safely run stuff in
threads for you.

Andrew

On Mon, Dec 24, 2018 at 7:24 PM Akshay Surya  wrote:

> Sorry for that Actually in my code I did use await before send, but
> somehow got deleted when I posted here... I would try debugging it but I am
> having a doubt that it's because django is inherently event driven... Is
> there something like a request response cycle should be synchronous that
> you can't send responses in batches? Should I try to do it like conduct the
> execution in threads and the results being pushed to channel layer in the
> background and using django signals to trigger a call whenever something is
> pushed to the channel and send it to a websocket connection... But wouldn't
> that require two websocket connections? One for requesting and another for
> receiving responses Or am I going about it the wrong way
>
> On Monday, December 24, 2018 at 8:36:24 PM UTC+5:30, Andrew Godwin wrote:
>>
>> You appear to have missed an "await" before "self.send" - could that be
>> the cause?
>>
>> In general, the best thing to do if things seem to "block" is to set the
>> environment variable PYTHONASYNCIODEBUG=1 and Python will warn you if
>> anything blocks for too long and seems synchronous.
>>
>> Andrew
>>
>> On Mon, Dec 24, 2018 at 2:24 PM Akshay Surya  wrote:
>>
>>>
>>>
>>> Hi everybody,
>>>
>>> I wrote django channels code to send api data from two different sources
>>> asynchronously through webscokets. The different sources takes few seconds
>>> to 1 minute to compute and send back the data. I managed to call them
>>> asynchronously using asyncio event loop. But the issue is that they are not
>>> sending the response back asynchronously. The code just waits for the all
>>> the data to arrive and sends everything at the same time.
>>>
>>> Channels Code:
>>>
>>> class SearchHotelConsumer(AsyncWebsocketConsumer):
>>> def __init__(self, *args, **kwargs):
>>> super().__init__(*args, **kwargs)
>>> self.source1 = Source1()
>>> self.source2 = Source2()
>>>
>>> async def connect(self):
>>> await self.accept()
>>>
>>> def disconnect(self, close_code):
>>> pass
>>>
>>> async def _source1_handler(self, request, queue):
>>> source1_response = await self.source1.async_post(request)
>>>
>>> await queue.put(source1_response.data)
>>>
>>> async def _source2_handler(self, request, queue):
>>> source2_response = await self.source2.async_post(request)
>>>
>>> await queue.put(source2_response.data)
>>>
>>> async def _send_msg(self, queue):
>>> while True:
>>> message = await queue.get()
>>> if message is None:
>>> break
>>> print('got the message')
>>> self.send(text_data=json.dumps({
>>> 'message': message
>>> }, cls=DjangoJSONEncoder))
>>>
>>> queue.task_done()
>>>
>>> def receive(self, text_data):
>>> text_data_json = json.loads(text_data)
>>> message = text_data_json['message']
>>>
>>> request = HttpRequest()
>>> request.method = 'POST'
>>> request.session = self.scope["session"]
>>> request = Request(request)
>>> for key, val in message.items():
>>> request.data[key] = val
>>>
>>> queue = asyncio.Queue()
>>> sender = asyncio.ensure_future(self._send_msg(queue))
>>>
>>> await self._source1_handler(request, queue)
>>> await self._source2_handler(request, queue)
>>>
>>> await queue.join()
>>>
>>> sender.cancel()
>>>
>>> How can I make the message sending part truly asynchronous?
>>>
>>> Regards
>>>
>>> --
>>> You received this message because you are subscrib

Re: Django channels communication not asynchronous

2018-12-24 Thread Andrew Godwin
You appear to have missed an "await" before "self.send" - could that be the
cause?

In general, the best thing to do if things seem to "block" is to set the
environment variable PYTHONASYNCIODEBUG=1 and Python will warn you if
anything blocks for too long and seems synchronous.

Andrew

On Mon, Dec 24, 2018 at 2:24 PM Akshay Surya  wrote:

>
>
> Hi everybody,
>
> I wrote django channels code to send api data from two different sources
> asynchronously through webscokets. The different sources takes few seconds
> to 1 minute to compute and send back the data. I managed to call them
> asynchronously using asyncio event loop. But the issue is that they are not
> sending the response back asynchronously. The code just waits for the all
> the data to arrive and sends everything at the same time.
>
> Channels Code:
>
> class SearchHotelConsumer(AsyncWebsocketConsumer):
> def __init__(self, *args, **kwargs):
> super().__init__(*args, **kwargs)
> self.source1 = Source1()
> self.source2 = Source2()
>
> async def connect(self):
> await self.accept()
>
> def disconnect(self, close_code):
> pass
>
> async def _source1_handler(self, request, queue):
> source1_response = await self.source1.async_post(request)
>
> await queue.put(source1_response.data)
>
> async def _source2_handler(self, request, queue):
> source2_response = await self.source2.async_post(request)
>
> await queue.put(source2_response.data)
>
> async def _send_msg(self, queue):
> while True:
> message = await queue.get()
> if message is None:
> break
> print('got the message')
> self.send(text_data=json.dumps({
> 'message': message
> }, cls=DjangoJSONEncoder))
>
> queue.task_done()
>
> def receive(self, text_data):
> text_data_json = json.loads(text_data)
> message = text_data_json['message']
>
> request = HttpRequest()
> request.method = 'POST'
> request.session = self.scope["session"]
> request = Request(request)
> for key, val in message.items():
> request.data[key] = val
>
> queue = asyncio.Queue()
> sender = asyncio.ensure_future(self._send_msg(queue))
>
> await self._source1_handler(request, queue)
> await self._source2_handler(request, queue)
>
> await queue.join()
>
> sender.cancel()
>
> How can I make the message sending part truly asynchronous?
>
> Regards
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAJXf11o_B7W3BiCpjKGEgEVynG2o5HoDRxY_EUFuJ%2B5Wbk5Yag%40mail.gmail.com
> 
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAFwN1ur1_f2FvCaK%3DwjLgZU1zcmbzXT7c8Qn%2Bvm-mPcWOUV1mg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Error when installing channels

2018-12-24 Thread Andrew Godwin
This is a general problem with installing compiled Python packages on
Windows. You'll need to install the compiler as outlined here:
https://wiki.python.org/moin/WindowsCompilers (or search for "python visual
c++ 14")

Andrew

On Sat, Dec 22, 2018 at 11:23 AM shiva kumar 
wrote:

> Hai guys, I am getting error when installing channels using pip.
>
> These is the error ' need Microsoft visual c++ 14.0'
>
> Can anyone suggest how to download and get it done. I tried to do it. I am
> unable to understand how to do and what to do.
>
> Pls respond to my issue.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAMsYeuHZvppFvWhgdfN8Xrn-abgMM368-Bq28wjw1nDXinsagQ%40mail.gmail.com
> 
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAFwN1ur-QuNqD3aTHYLq0yt3nsdCdzt1DWm2UkrYyBwp4d7YXQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Error of channels

2018-12-17 Thread Andrew Godwin
I've seen a lot of people have a similar error but I'm never sure what
causes it - that said, I generally advise them to check their Redis logs to
see if Redis is OK, as that could cause this?

Andrew

On Mon, Dec 17, 2018 at 11:36 AM  wrote:

> I had an error while build a chatroom useing django-channels:
>
> I tried follow the official website: https://channels.readthedocs.io
>
> but while i build a room to chat with many people, an error occered:
>
> Exception inside application: Reader at end of file
>   File
> "/home/lujianxin/VirtualEnvs/demo/lib/python3.7/site-packages/channels/sessions.py",
> line 179, in __call__
> return await self.inner(receive, self.send)
>   File
> "/home/lujianxin/VirtualEnvs/demo/lib/python3.7/site-packages/channels/middleware.py",
> line 41, in coroutine_call
> await inner_instance(receive, send)
>   File
> "/home/lujianxin/VirtualEnvs/demo/lib/python3.7/site-packages/channels/consumer.py",
> line 59, in __call__
> [receive, self.channel_receive], self.dispatch
>   File
> "/home/lujianxin/VirtualEnvs/demo/lib/python3.7/site-packages/channels/utils.py",
> line 52, in await_many_dispatch
> await dispatch(result)
>   File
> "/home/lujianxin/VirtualEnvs/demo/lib/python3.7/site-packages/channels/consumer.py",
> line 73, in dispatch
> await handler(message)
>   File
> "/home/lujianxin/VirtualEnvs/demo/lib/python3.7/site-packages/channels/generic/websocket.py",
> line 196, in websocket_receive
> await self.receive(text_data=message["text"])
>   File "/home/lujianxin/Files/PycharmProjects/demo/chat/consumers.py",
> line 44, in receive
> 'message': message
>   File
> "/home/lujianxin/VirtualEnvs/demo/lib/python3.7/site-packages/channels_redis/core.py",
> line 611, in group_send
> key, min=0, max=int(time.time()) - self.group_expiry
>   File
> "/home/lujianxin/VirtualEnvs/demo/lib/python3.7/site-packages/aioredis/commands/sorted_set.py",
> line 268, in zremrangebyscore
> return self.execute(b'ZREMRANGEBYSCORE', key, min, max)
>   File
> "/home/lujianxin/VirtualEnvs/demo/lib/python3.7/site-packages/aioredis/commands/__init__.py",
> line 51, in execute
> return self._pool_or_conn.execute(command, *args, **kwargs)
>   File
> "/home/lujianxin/VirtualEnvs/demo/lib/python3.7/site-packages/aioredis/connection.py",
> line 319, in execute
> raise ConnectionClosedError(msg)
>   Reader at end of file
> WebSocket DISCONNECT /ws/chat/ljx/ [192.168.2.126:41154]
>
> if i just build a echo server for one connection, it works ok, but while i
> use channels and group_send, it worked ok when i send one or two message,
> all of memeber of the room received message, while i send more the problem
> raising, i promissed that i followed the lesson and my settings,
> environment is ok, but what caused this error?
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/210770a5-b587-486d-840c-56e3deab160a%40googlegroups.com
> 
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAFwN1urnOyULa1o0GBEqfoRT_8YnUPR3y6umwuiL8goNS80u7w%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Channels: development issue with multiple daphne workers

2018-11-27 Thread Andrew Godwin
I'm not sure why you're getting the  _UnixSelectorEventLoop - that sounds
like a Twisted error you should search around for, and make sure you have
the right package versions.

--fd 0 should not be needed in that command line, since you're already
passing the UNIX socket, I don't remember why it's there.

I suggest you remove fd=0 and see if that then works with multiple
processes.

Andrew

On Mon, Nov 26, 2018 at 3:10 PM Zhiyu (Drew) Li  wrote:

> Hi there,
>
> I am trying to migrate a tornado project to django channel2. I have moved
> all essential parts and wired them up in channel. It runs OK in development
> mode, but in production it seems the multi-daphne worker configuration is
> causing strange errors.
> AttributeError: '_UnixSelectorEventLoop' object has no attribute
> 'remove_timeout'
>
> I followed instructions on the official doc (
> https://channels.readthedocs.io/en/latest/deploying.html) to set up
> supervisor and nginx. But I am still lack of understanding how it works.
> Cloud you please explain more on the following settings?
>
>
> *# TCP socket used by Nginx backend upstream*
> *socket=tcp://localhost:8000*
>
> *# Each process needs to have a separate socket file, so we use
> process_num*
> *# Make sure to update "mysite.asgi" to match your project name*
> *command=daphne -u /run/daphne/daphne%(process_num)d.sock --fd 0
> --access-log - --proxy-headers mysite.asgi:application*
>
> *# Number of processes to startup, roughly the number of CPUs you have*
> *numprocs=4*
>
> *Questions:*
> What is "--fd 0" here? Is "0" the file descriptor for socket
> "tcp://localhost:8000" that Nginx is proxying to?
> What is "-u /run/daphne/daphne%(process_num)d.sock" then? Why do we need
> to set up separate socket for each daphne process? Are all the daphne
> processes already talking to "fd 0"?
>
> My website only works if I change either of the following
> A) change to numprocs=1 ;
> Or
> B) remove "-u /run/daphne/daphne%(process_num)d.sock"
>
> Thanks
> Drew
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAMmsbU%3D-oPX_yZ7Kx8PmVfjwKJNSSC0hKOEUXyRGztzHogKGzA%40mail.gmail.com
> 
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAFwN1uq88vgWuJDuyrc7o7tEdu1OT%2BU-i6yXZ2JuCEUD2%3DXeiw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Django with Channels 2.1.1 Error During WebSocket handshake 404

2018-11-16 Thread Andrew Godwin
You cannot use gunicorn to run WebSockets or other async code - you need an
ASGI server, like Daphne or uvicorn. Switch to one of those and it should
start working.

Andrew

On Fri, Nov 16, 2018 at 8:11 PM Robert Fox  wrote:

> I am working on a project using Django and Channels 2.1.1. Everything is
> working splendidly on my home PC but when I deploy to AWS I get the error:
> 'WebSocket connection to 'ws://IP/ws/lobby/' failed: Error during WebSocket
> handshake: Unexpected response code: 404 ' which originates from the line
> in my HTML where I establish the chat socket :
> var chatSocket = new WebSocket(
> 'ws://' + window.location.host + '/ws/' + roomName + '/'
> );
> I am using nginx and gunicorn to run my Django application on the host
> machine.
> When I went to the Channels 2 tutorial it has a section for testing
> communication between the channels layer and Redis using the
> Django Shell which I tested and works.
> My full code can be seen at https://github.com/Rob-Fox/Chat2
>
> Thank you
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/16ed620b-c05f-4392-8d84-6cbd85436ca2%40googlegroups.com
> 
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAFwN1uq_EsgV2jt-PzMFsL1%2BAnQqEX0r4XHjLKPa5D_XZcfi4w%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Channels + django-tenants

2018-11-14 Thread Andrew Godwin
Hm, that's not sufficiently better than just supplying a new middleware to
be honest. The sort of hooks I prefer are where you can subclass and
improve what we ship with and give the user a new, non-fragile one, whereas
that just sounds like it's injecting a function in.

Honestly, given Channels is currently only one maintainer, any increase in
surface area is unlikely unless there's a very good reason. Channels' auth
stuff is just standard Django code + settings for the most part, so there's
already bits you can reuse to do most of the work.

Andrew

On Tue, Nov 13, 2018 at 3:00 PM  wrote:

> I know this is not general, but maybe `get_user` could accept an optional
> function as second parameter, so that the function could wrap the inner
> content of the current `get_user`. django-tenants provides a regular
> middleware, but I think the async breaks any attempt to handle this through
> a channels-like middleware.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/f99a231f-cdc5-426f-8d0a-20c0f8ebc2fc%40googlegroups.com
> 
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAFwN1uor2Y0xdzogVEgZLM54qRrnuDAuKaN3T_5YkOO5nZCt3g%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Channels + django-tenants

2018-11-13 Thread Andrew Godwin
What sort of hook would you be imagining? The DB connection stuff is deep
inside Django itself, rather than in Channels, so I'm not sure what
sensible top-level thing we could provide that would make sense.

Andrew

On Tue, Nov 13, 2018 at 2:16 PM  wrote:

> Django-tenants is a package to provide multi-tenancy in Django through
> Postgres schemas (https://github.com/tomturner/django-tenants/)
>
> Django-tenants relies on setting the schema on the connection in order to
> make queries to the right Postgres schema. When you want to secure your
> consumers with user permissions, django-tenants needs to be properly routed
> in order to retrieve the right users from database, otherwise the user in
> scope is not properly populated for permission checking.
>
> In order to overcome this, I have redefined (in a not DRY way, aka
> copy/paste) `get_user`, `TenantAuthMiddleware` and
> `TenantAuthMiddlewareStack`, just because I basically need to wrap
> `get_user` inside `with scope["tenant"]` (provided I put the tenant in the
> scope before, in a custom router.)
>
> Is there any way to provide a hook so this can be achieved in a DRY way?
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/c547563c-a8a6-416c-bf59-54907f512c77%40googlegroups.com
> 
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAFwN1uraywimYgY4BmsqxMf8n921WJ0-ZZLLLNJYcd%3D%3Dpaz%3DOA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Channels group_send hangs in extrenal process

2018-11-13 Thread Andrew Godwin
I've never tried using the async-to-sync stuff with multiprocessing -
first, I'd try removing that and see if it still works (just run from a
management command or script directly).

If it still fails, I'd suggest running a redis MONITOR to see if any
traffic makes it to Redis at all.

Andrew

On Tue, Nov 13, 2018 at 2:07 PM Yaro Kifor  wrote:

> Hello,
>
> I am a little new to channels, but I can't seem to figure out this issue.
> I am trying to use group_send from an external process and I am using Redis
> as my channel backend. Here is what my process code looks like:
>
>
> from channels.layers import get_channel_layer
> from multiprocessing import Process
> from asgiref.sync import AsyncToSync, SyncToAsync
>
> class GenericAgent(Process):
> def __init__(self, *args, **kwargs):
> self.channel_layer = get_channel_layer()
> self.send_message = AsyncToSync(self.channel_layer.group_send)
> super().__init__(*args, **kwargs)
>
> def run(self):
> while True:
> print("Sending data")
> self.send_message('test', {'test': 0})
>time.sleep(1)
>
>
> Anytime I run the process it will print out "Sending data" once and do
> nothing else. Any channel that is part of the test group will not receive
> anything as well. If anyone has a clue to what the issue might be please
> let know your help is greatly appreciated.
>
> Here is my pip freeze:
>
> aioredis==1.2.0
> asgiref==2.3.2
> async-timeout==3.0.1
> attrs==18.2.0
> autobahn==18.11.1
> Automat==0.7.0
> certifi==2018.4.16
> channels==2.1.5
> channels-redis==2.3.1
> chardet==3.0.4
> constantly==15.1.0
> daphne==2.2.3
> Django==2.0.6
> easy-thumbnails==2.5
> future==0.15.2
> hiredis==0.2.0
> hyperlink==18.0.0
> idna==2.7
> imageio==2.3.0
> incremental==17.5.0
> interop==0.0.0
> LatLon23==1.0.7
> monotonic==1.2
> msgpack==0.5.6
> numpy==1.14.4
> Pillow==5.1.0
> psycopg2-binary==2.7.5
> PyHamcrest==1.9.0
> pyproj==1.9.5.1
> pytz==2018.4
> rawpy==0.10.1
> requests==2.19.1
> six==1.11.0
> sorl-thumbnail==12.4.1
> Twisted==18.9.0
> txaio==18.8.1
> urllib3==1.23
> zope.interface==4.6.0
>
>
> Thanks in advance,
> Yaro
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/4916f184-9db9-4d34-a9cd-3d6ff5238aff%40googlegroups.com
> 
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAFwN1upK-iXuxbZNTcPu78OzPnDAfS8-E%3DfOHnPUio0u1dppRw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Channels: Test case fails on async.sleep(X>0)

2018-11-13 Thread Andrew Godwin
Couple of points:

- Did you install pytest-django? That should help the settings thing.
- There's a default 1-second timeout on get_response; pass timeout=4 to it
and you should see that error go away.

We have a default timeout because otherwise we can't tell if an async app
has died/crashed and it'd hang forever!

Andrew

On Mon, Nov 12, 2018 at 3:43 PM Zhiyu (Drew) Li  wrote:

> Hi there,
>
> I was trying to write a test for a consumer. It is just a very simply
> AsyncHttpConsumer subclass that awaits on asyncio.sleep(3) and returns a
> "OK" in plain text.
>
> The test case is:
> @pytest.mark.asyncio
> async def test_my_consumer():
> communicator = HttpCommunicator(BasicHttpConsumer, "GET", "asynchttp")
> response = await communicator.get_response()
> assert response["status"] == 200
> assert response["body"] == b"OK"
>
> 1) Error 1:
> I run it with command "pytest .py"
> The first error I got was:
> django.core.exceptions.ImproperlyConfigured: Requested settings, but
> settings are not configured. You must either define the environment
> variable DJANGO_SETTINGS_MODULE or call settings.configure() before
> accessing settings.
>
> I google online and found this fix:
> os.environ.setdefault("DJANGO_SETTINGS_MODULE", "MODULE_PATH_TO.settings")
>
>
> 2) Error 2:
> The new error I am getting is:
> self = , exc_type =  'concurrent.futures._base.CancelledError'>
>
> def _do_exit(self, exc_type: Type[BaseException]) -> None:
> if exc_type is asyncio.CancelledError and self._cancelled:
> self._cancel_handler = None
> self._task = None
> >   raise asyncio.TimeoutError
> E   concurrent.futures._base.TimeoutError
>
> But If I change async.sleep(3) to  async.sleep(0) the test passed. Not
> sure why
>
> Also how to test url mapping works correctly?
>
> Thanks
> Drew
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAMmsbUk-3AemjcTxxidf4GBvKeWG7b72qwfUcs5owReTeOEMiw%40mail.gmail.com
> 
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAFwN1uq02V6p53P84z0kJo13jrVWDtAr7_qoZ69L1cntQXww6w%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Django channels worker loses some messages

2018-11-11 Thread Andrew Godwin
Are you using Channels 1 or Channels 2? And which channel layer are you
using?

Andrew

On Sun, Nov 11, 2018 at 5:05 AM Parsa Banihashemi <
parsa.banihash...@gmail.com> wrote:

> Hi,
> I have a simple sync consumer which receives websocket messages from the
> client, and each time, It passes a message to a worker channel to process
> it.
> A LOT of the messages are just simply lost and not processed.
> I have a print ("sent") whenever a message is sent to the worker channel.
> I have another print("received") statement at the beginning of worker's
> method.
> If I send tasks to the worker more frequently that every 2-3 seconds, the
> worker simply just ignores them. I see the "sent" in main stdout, but not
> received in the worker's stdout
> I have to wait 2-3 seconds and send another message to see "received"
> statement in stdout
>
> That is so strange and disappointing. I am debugging on Windows Pycharm,
> and running the worker process correctly I guess
>
> I appreciate any insights
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/53dbaf3e-5bee-4a54-a4b5-51be2bcf60d3%40googlegroups.com
> 
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAFwN1urO-p61bu5i%2B4g4w0OVdaWZwWU8XS9XGCFNXK-Pu3qHwQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Channel: Migrate regular Django project to Channels2

2018-11-07 Thread Andrew Godwin
Channels doesn't take over at all unless you configure an async consumer.
To make sure it's working I'd recommend writing a single async consumer,
and running a test against that to ensure it works.

Andrew

On Wed, Nov 7, 2018 at 1:11 PM Zhiyu (Drew) Li  wrote:

> Hi there,
>
> I am trying to migrate an existing Django project to Channels2. We want to
> keep all existing sync codes unchanged and we will continue mode of the
> development in sync mode. But having Channels opens the opportunity to
> develop some features in async mode.
>
> I was able to install channels2 and get it configured. The portal still
> runs and all tests passed. But does it mean the migration is successful?
> How can I know if the tests were actually run in the channels2 env?
>
> Anything particular I need to double check? like a migration checklist
>
> Thanks
> Drew
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAMmsbU%3DzsYB5J7p4_mC1Gj3HqjiLRLo7Hc4UhaHEN_FXRv0ScA%40mail.gmail.com
> 
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAFwN1uqEeB%2BmdUQ8DYbUFfdRBCqr7Bv5R_2GqT9mb53buovdBg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Adding Channels to and Existing Django Application

2018-11-06 Thread Andrew Godwin
I'm afraid I don't have an easy example to hand - maybe someone else does.
It sounds like you might have something odd in your settings, models.py, or
other files that load at startup.

Andrew

On Tue, Nov 6, 2018 at 5:31 AM  wrote:

> even down to running two different servers for that project, one normal
>> WSGI one and one ASGI one to let you do async things.
>
>
> I've tried doing that, but failed. I've created a asgi.py file and tried
> to run it with daphne, but the server didn't start. Daphne simply hang
> there. To my surprise, the same asgi.py file works fine on a completely
> independent django project. My best guess is that something on the
> convoluted settings.py is messing up with daphne.
>
> Are you aware of a project that does what you've mentioned? Would you
> point me to it? I think that looking at the config files could help me set
> this up.
>
> Juarez
>
> On Monday, November 5, 2018 at 9:24:39 PM UTC-2, Andrew Godwin wrote:
>>
>> Hey,
>>
>> You can just add channels to an existing Django project and only use it
>> for the bits you need it for - even down to running two different servers
>> for that project, one normal WSGI one and one ASGI one to let you do async
>> things.
>>
>> You shouldn't migrate any existing code to channels - just add it and
>> then use it for the new bits you need it for, basically.
>>
>> Andrew
>>
>> On Sun, Nov 4, 2018 at 1:16 PM  wrote:
>>
>>> Greetings y'all!
>>>
>>> I'm looking for recommendations regarding adding Channels to an existing
>>> Django 1.11 application.
>>>
>>> Every guide I've followed assumes that one is using Channels to build an
>>> application from scratch, and that is OK to replace django's native calls
>>> by Channels.
>>>
>>> Channels' documentation suggests different ways of setting up
>>> authentication, sessions, etc, but it is not clear to me that every feature
>>> of Django 1.11 has its counterpart
>>> on Channels. I am afraid I'll spend days migrating my system to Channels
>>> only to figure out that a particular feature of Django is not supported by
>>> Channels. In order to avoid that I've decided to ask y'all first.
>>>
>>> Ideally, I would like to set Channels on a particular Django app, not on
>>> the entire Django project (other apps would not be aware of Channels being
>>> setup). Is that possible?
>>>
>>> If this is not possible, my best guess is that I would need to add
>>> Channels to an independent Django project, and have these two servers
>>> communicate. This 2nd server would point to the same database as the 1st,
>>> and thus would be able to run authentication with same credentials, among
>>> other data related operations.
>>>
>>> I appreciate any guidance on this matter.
>>>
>>> --
>>> You received this message because you are subscribed to the Google
>>> Groups "Django users" group.
>>> To unsubscribe from this group and stop receiving emails from it, send
>>> an email to django-users...@googlegroups.com.
>>> To post to this group, send email to django...@googlegroups.com.
>>> Visit this group at https://groups.google.com/group/django-users.
>>> To view this discussion on the web visit
>>> https://groups.google.com/d/msgid/django-users/a8c4164a-ffbb-4975-8d48-f8f058466b4d%40googlegroups.com
>>> <https://groups.google.com/d/msgid/django-users/a8c4164a-ffbb-4975-8d48-f8f058466b4d%40googlegroups.com?utm_medium=email_source=footer>
>>> .
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/5cba75bd-19cc-4a10-97bc-e85ffc51cca3%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/5cba75bd-19cc-4a10-97bc-e85ffc51cca3%40googlegroups.com?utm_medium=email_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAFwN1uqjQzKmzDoHhXE-M5a1B-XuJ7tRhpjhvJr%2Bf056Lck9DQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Channels: event loop and thread

2018-11-06 Thread Andrew Godwin
Your assumptions are correct. The only extra thing to be aware of is that
some Django interactions (like the Channels Auth middleware) need to do
database connections and thus have to launch into a subthread for that, but
that's also managed.

Andrew

On Tue, Nov 6, 2018 at 4:41 AM Zhiyu (Drew) Li  wrote:

> Hi there,
>
> I got the following 2 questions
>
> There is only one thread (MainThread) and one event loop running on it by
> default if all existing consumers are asynchronous consumers?
>
> Regular sync views (if any) are handled by Threadpoolexecutor and run in a
> sub Thread. These sync views won't block the event loop in the main thread.
> Channels takes care of this subthread and I don't need to explicitly manage
> it. Right?
>
> Thanks
> Drew
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAMmsbUkjbuRvYjoZDyDY%3DXkaoQJHHiG659XKX-tPCbe7HqgivA%40mail.gmail.com
> 
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAFwN1urN2bWHedWU1YfpXYmGiZ8z%2BAdE4PUX_sitTtD6-EriVQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Adding Channels to and Existing Django Application

2018-11-05 Thread Andrew Godwin
Hey,

You can just add channels to an existing Django project and only use it for
the bits you need it for - even down to running two different servers for
that project, one normal WSGI one and one ASGI one to let you do async
things.

You shouldn't migrate any existing code to channels - just add it and then
use it for the new bits you need it for, basically.

Andrew

On Sun, Nov 4, 2018 at 1:16 PM  wrote:

> Greetings y'all!
>
> I'm looking for recommendations regarding adding Channels to an existing
> Django 1.11 application.
>
> Every guide I've followed assumes that one is using Channels to build an
> application from scratch, and that is OK to replace django's native calls
> by Channels.
>
> Channels' documentation suggests different ways of setting up
> authentication, sessions, etc, but it is not clear to me that every feature
> of Django 1.11 has its counterpart
> on Channels. I am afraid I'll spend days migrating my system to Channels
> only to figure out that a particular feature of Django is not supported by
> Channels. In order to avoid that I've decided to ask y'all first.
>
> Ideally, I would like to set Channels on a particular Django app, not on
> the entire Django project (other apps would not be aware of Channels being
> setup). Is that possible?
>
> If this is not possible, my best guess is that I would need to add
> Channels to an independent Django project, and have these two servers
> communicate. This 2nd server would point to the same database as the 1st,
> and thus would be able to run authentication with same credentials, among
> other data related operations.
>
> I appreciate any guidance on this matter.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/a8c4164a-ffbb-4975-8d48-f8f058466b4d%40googlegroups.com
> 
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAFwN1uprrm2fVuC6mpX6OdHWPb55S1v%3DT%2BJP8vss_%3Dz1_p2DMA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: How to subclass AsyncHttpConsumer for a GET request?

2018-10-30 Thread Andrew Godwin
You need to route by URL - see the example in the docs here, including how
to fallback to Django views:
https://channels.readthedocs.io/en/latest/topics/routing.html#urlrouter

Andrew

On Tue, Oct 30, 2018 at 5:05 AM Zhiyu (Drew) Li  wrote:

> Thanks! Another question it seems once I manually put 'http':
> MyAsyncHttpConsumer pair in the ProtocolRouter, all existing sync http
> views stopped running as their type is "http" as well. So how to do a mix
> of async HTTP and sync http?
> Thanks
> Drew
>
> On Mon, Oct 29, 2018, 23:15 Andrew Godwin  wrote:
>
>> Yup, that's the right way - subclass the async consumer class and then
>> write a handle method. You have to do your own post/get distinctions, like
>> in a Django view, but with the scope rather than the request.
>>
>> Andrew
>>
>> On Mon, Oct 29, 2018 at 4:37 PM Zhiyu/Drew Li  wrote:
>>
>>> Not sure if this is the best way. I just found
>>> inside AsyncHttpConsumer.handle() I can access self.scope['method'] to
>>> determine if it is a GET or POST or others.
>>>
>>> Thanks
>>> Drew
>>>
>>>
>>>
>>>
>>>
>>>
>>> On Monday, October 29, 2018 at 3:50:43 PM UTC-6, Zhiyu/Drew Li wrote:
>>>>
>>>> Hi there,
>>>>
>>>> Newbie to Channels.
>>>>
>>>> I am trying to write a Async consumer to handle a http GET request
>>>> How to write a subclass MyAsynHttpConsumer(AsyncHttpConsumer) for this
>>>> purpose? Or I am looking at the wrong class?
>>>>
>>>> Also if I understand correctly, I should manually add a new pair
>>>> 'http': MyAsynHttpConsumer to ProtocolTypeRouter()
>>>>
>>>> async_http_urlpatterns = [
>>>> url(r'^async-http/$', consumers. MyAsynHttpConsumer   ),
>>>> ]
>>>>
>>>> ProtocolTypeRouter (
>>>> 'http': AuthMiddlewareStack(
>>>> URLRouter(
>>>>  my_channels_app.routing.async_http_urlpatterns
>>>> )
>>>> ),
>>>> )
>>>>
>>>> Thanks
>>>> Drew
>>>>
>>>>
>>>>
>>>> --
>>> You received this message because you are subscribed to the Google
>>> Groups "Django users" group.
>>> To unsubscribe from this group and stop receiving emails from it, send
>>> an email to django-users+unsubscr...@googlegroups.com.
>>> To post to this group, send email to django-users@googlegroups.com.
>>> Visit this group at https://groups.google.com/group/django-users.
>>> To view this discussion on the web visit
>>> https://groups.google.com/d/msgid/django-users/8c46871d-6e24-47e1-8813-5721014aedca%40googlegroups.com
>>> <https://groups.google.com/d/msgid/django-users/8c46871d-6e24-47e1-8813-5721014aedca%40googlegroups.com?utm_medium=email_source=footer>
>>> .
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>> --
>> You received this message because you are subscribed to a topic in the
>> Google Groups "Django users" group.
>> To unsubscribe from this topic, visit
>> https://groups.google.com/d/topic/django-users/zQcvIvqapCo/unsubscribe.
>> To unsubscribe from this group and all its topics, send an email to
>> django-users+unsubscr...@googlegroups.com.
>> To post to this group, send email to django-users@googlegroups.com.
>> Visit this group at https://groups.google.com/group/django-users.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/django-users/CAFwN1uopgpGn7uASW0%2BteonKN8qQFdBXcwA2N8_JM1-CTYiDdg%40mail.gmail.com
>> <https://groups.google.com/d/msgid/django-users/CAFwN1uopgpGn7uASW0%2BteonKN8qQFdBXcwA2N8_JM1-CTYiDdg%40mail.gmail.com?utm_medium=email_source=footer>
>> .
>> For more options, visit https://groups.google.com/d/optout.
>>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAMmsbUnQC-MFDHCV2Py1B_BajRRHDQJGjkOowSMBcNOULfTx%3DA%40mail.gmail.com
> <https://groups.google.com/d/msgid/django-users/CAMmsbUnQC-MFDHCV2Py1B_BajRRHDQJGjkOowSMBcNOULfTx%3DA%40mail.gmail.com?utm_medium=email_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAFwN1uq0-9SusXtrQkw1GaN_Cv37QdA99-sGSo4Sg0vcccbhmQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: How to subclass AsyncHttpConsumer for a GET request?

2018-10-29 Thread Andrew Godwin
Yup, that's the right way - subclass the async consumer class and then
write a handle method. You have to do your own post/get distinctions, like
in a Django view, but with the scope rather than the request.

Andrew

On Mon, Oct 29, 2018 at 4:37 PM Zhiyu/Drew Li  wrote:

> Not sure if this is the best way. I just found
> inside AsyncHttpConsumer.handle() I can access self.scope['method'] to
> determine if it is a GET or POST or others.
>
> Thanks
> Drew
>
>
>
>
>
>
> On Monday, October 29, 2018 at 3:50:43 PM UTC-6, Zhiyu/Drew Li wrote:
>>
>> Hi there,
>>
>> Newbie to Channels.
>>
>> I am trying to write a Async consumer to handle a http GET request
>> How to write a subclass MyAsynHttpConsumer(AsyncHttpConsumer) for this
>> purpose? Or I am looking at the wrong class?
>>
>> Also if I understand correctly, I should manually add a new pair 'http':
>> MyAsynHttpConsumer to ProtocolTypeRouter()
>>
>> async_http_urlpatterns = [
>> url(r'^async-http/$', consumers. MyAsynHttpConsumer   ),
>> ]
>>
>> ProtocolTypeRouter (
>> 'http': AuthMiddlewareStack(
>> URLRouter(
>>  my_channels_app.routing.async_http_urlpatterns
>> )
>> ),
>> )
>>
>> Thanks
>> Drew
>>
>>
>>
>> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/8c46871d-6e24-47e1-8813-5721014aedca%40googlegroups.com
> 
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAFwN1uopgpGn7uASW0%2BteonKN8qQFdBXcwA2N8_JM1-CTYiDdg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Channels -- a question about how ProtocolTypeRouter works

2018-10-26 Thread Andrew Godwin
I would instead think of the object that ends up there as a factory that
makes the final objects that end up per-scope. It's slightly confusing, I
will grant you, but in Python anything can make a new class instance, not
just a class constructor, which is what's happening here.

Andrew

On Fri, Oct 26, 2018 at 3:17 PM Zhiyu (Drew) Li  wrote:

> Thanks!  So that means some objects last across multiple scopes, and they
> can create another obj that only lasts for one specific scope?
> I am still confused how the different layers of middleware wrap around the
> real asgi application -- a obj just lasts for one scope.
> I need to go through the code deeper.
>
> Thanks
> Drew
>
>
>
> On Fri, Oct 26, 2018 at 4:07 PM Andrew Godwin  wrote:
>
>> Hi Drew,
>>
>> The return value is indeed an object, but if you look closely at it
>> you'll see it defines a __call__ method - meaning that when it's called as
>> an ASGI application, it then returns a further object to act inside the
>> scope.
>>
>> Andrew
>>
>> On Fri, Oct 26, 2018 at 2:55 PM Zhiyu/Drew Li  wrote:
>>
>>> Hi,
>>>
>>> Newbie here. (please bear with me if this a stupid question)
>>>
>>> I am going through the tutorial and doc and got this question regarding how 
>>> ProtocolTypeRouter works.
>>> ProtocolTypeRouter.__init__ takes a dict "application_mapping" and append a 
>>> key-value pair to it: "http': class AsgiHandler
>>> As doc says ASGI application is created on a per scope basis, I think I 
>>> vaguely understand how the "http" one works.
>>> Once a http scope comes in, an instance/object of class AsgiHandler is 
>>> initiated, which lasts for the duration of the scope.
>>> So it is one scope leads to one ASGI application/object
>>>
>>> But I am confused about this one:
>>> application = ProtocolTypeRouter({
>>> # (http->django views is added by default)
>>> 'websocket': AuthMiddlewareStack(
>>> URLRouter(
>>> chat.routing.websocket_urlpatterns
>>> )
>>> ),
>>> })
>>> The whole value of this key-value pair 
>>> AuthMiddlewareStack(URLRouter(chat.routing.websocket_urlpatterns)) seems to 
>>> be a object not a class,
>>> so that means the application already exists before a websocket scope comes 
>>> in. How does this conform to "one scope one application" rule?
>>>
>>> Any help would be appreciated.
>>>
>>> Thanks
>>> Drew
>>>
>>>
>>> --
>>> You received this message because you are subscribed to the Google
>>> Groups "Django users" group.
>>> To unsubscribe from this group and stop receiving emails from it, send
>>> an email to django-users+unsubscr...@googlegroups.com.
>>> To post to this group, send email to django-users@googlegroups.com.
>>> Visit this group at https://groups.google.com/group/django-users.
>>> To view this discussion on the web visit
>>> https://groups.google.com/d/msgid/django-users/f6088bd4-457a-4106-b883-332af9a99337%40googlegroups.com
>>> <https://groups.google.com/d/msgid/django-users/f6088bd4-457a-4106-b883-332af9a99337%40googlegroups.com?utm_medium=email_source=footer>
>>> .
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Django users" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to django-users+unsubscr...@googlegroups.com.
>> To post to this group, send email to django-users@googlegroups.com.
>> Visit this group at https://groups.google.com/group/django-users.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/django-users/CAFwN1urusiZjfH2HSnkaRcuNVUXMgLuLRxs%3DxH9tK%3D35enFDow%40mail.gmail.com
>> <https://groups.google.com/d/msgid/django-users/CAFwN1urusiZjfH2HSnkaRcuNVUXMgLuLRxs%3DxH9tK%3D35enFDow%40mail.gmail.com?utm_medium=email_source=footer>
>> .
>> For more options, visit https://groups.google.com/d/optout.
>>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view t

Re: Channels -- a question about how ProtocolTypeRouter works

2018-10-26 Thread Andrew Godwin
Hi Drew,

The return value is indeed an object, but if you look closely at it you'll
see it defines a __call__ method - meaning that when it's called as an ASGI
application, it then returns a further object to act inside the scope.

Andrew

On Fri, Oct 26, 2018 at 2:55 PM Zhiyu/Drew Li  wrote:

> Hi,
>
> Newbie here. (please bear with me if this a stupid question)
>
> I am going through the tutorial and doc and got this question regarding how 
> ProtocolTypeRouter works.
> ProtocolTypeRouter.__init__ takes a dict "application_mapping" and append a 
> key-value pair to it: "http': class AsgiHandler
> As doc says ASGI application is created on a per scope basis, I think I 
> vaguely understand how the "http" one works.
> Once a http scope comes in, an instance/object of class AsgiHandler is 
> initiated, which lasts for the duration of the scope.
> So it is one scope leads to one ASGI application/object
>
> But I am confused about this one:
> application = ProtocolTypeRouter({
> # (http->django views is added by default)
> 'websocket': AuthMiddlewareStack(
> URLRouter(
> chat.routing.websocket_urlpatterns
> )
> ),
> })
> The whole value of this key-value pair 
> AuthMiddlewareStack(URLRouter(chat.routing.websocket_urlpatterns)) seems to 
> be a object not a class,
> so that means the application already exists before a websocket scope comes 
> in. How does this conform to "one scope one application" rule?
>
> Any help would be appreciated.
>
> Thanks
> Drew
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/f6088bd4-457a-4106-b883-332af9a99337%40googlegroups.com
> 
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAFwN1urusiZjfH2HSnkaRcuNVUXMgLuLRxs%3DxH9tK%3D35enFDow%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Channels: about max message size

2018-10-23 Thread Andrew Godwin
Hmm, it's possible the 0.x series didn't have response streaming for files
so there'd be a limit, but I honestly can't remember, that was years ago.
There's really not much I can do other than recommending an upgrade to
something vaguely recent.

Also make sure Heroku is not cutting you off with a request timeout or
something.

Andrew

On Tue, Oct 23, 2018 at 5:38 AM Chris  wrote:

> I use Channels & websockets for my webapp's "export data" functionality,
> since it can take a long time to generate the file (more than 30 seconds).
> In general this works fine, even for large downloads (several MB), but I
> have seen several cases on Heroku where the browser never receives the
> message with the file, even though debugging shows that
> JsonWebsocketConsumer.send was called as expected.
>
> I found this Github issue  that
> says the message limit is 1 MB (or maybe 256 kb). My questions:
>
> - Where is this limit defined? I searched the source of Channels and
> Daphne. I'm trying to figure out where the failure is happening -- in the
> worker, Daphne, or Redis.
> - What happens if the limit is exceeded? Is an exception raised? (Because
> in my case it fails silently.)
> - Is there really a 1 MB limit? Because it seems I have been able to send
> files larger than that using channels/asgi_redis (unfortunately I'm still
> on channels 0.17.3, can't upgrade yet because of a Twisted bug).
>
> Thanks!
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/da8fc1ab-8a75-4504-ab3a-6ab31cfd132e%40googlegroups.com
> 
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAFwN1up4d8i%3DC-t2T_AYqmPYAsaQvNPjqCvn1MHJdwakfJvtNQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: [channels] 2.1.4 - Removing Javascript library entirely

2018-10-21 Thread Andrew Godwin
That's about right - you should ship and maintain the file yourself now.

Andrew

On Sun, 21 Oct 2018, 09:04 kradem,  wrote:

> Hi everyone!
>
> The subject is related to
> https://github.com/django/channels/commit/2a9d764ad03927581aa2bfcadccc3e953949cb98
>
> > It was very unmaintained, and having it in here sends the wrong message
> about it being useful and reliable.
>
> I used to include the javascript in my templates with:
>
> 
>
> I suppose the only change I should do now is to include the javascript
> file from the 2.1.3 channels version in my project static folder and
> reference it with (if js is the folder for my project's javascript)::
>
> 
>
> Am I missing something else here?
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/e5a5eca8-54f1-49f8-9159-f7b7c4de847e%40googlegroups.com
> 
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAFwN1uoZkwbKwV6kX12hWGrE3VtyTM1tb2V-7tti38qdn4vgqA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Channels: rejecting web socket connections with a custom close code

2018-10-06 Thread Andrew Godwin
Hi Matt,

This is because the two ways of closing are different - closing before the
socket is open sends a HTTP error code (what browsers report for this is
different in terms of websocket errors), whereas closing once it is open
allows a websocket error to be used.

This is referenced in the spec at
https://asgi.readthedocs.io/en/latest/specs/www.html#websocket but somewhat
obliquely; I’ll try to make it a bit clearer.

Andrew

On Wed, 3 Oct 2018 at 13:23, Matt F  wrote:

> Hello all,
>
> I have a question about the process of accepting/rejecting web socket
> connections in Channels consumers (v2.1.3).  I have found that if I have
> code like this:
>
> class MyConsumer(JsonWebsocketConsumer):
> def connect(self):
> if authenticated:
> self.accept()
> else:
> self.close(4001)
>
>
> then when rejecting a connection the close code always comes through on
> the client side as 1006 (abnormal closure) instead of 4001.  There is a
> stack overflow post about the same issue that led me to an answer that
> solves this:
> https://stackoverflow.com/questions/50009440/django-channels-2-0-websocketbridge-js-403-when-self-codecode-1007
>
> And a code snippet that works correctly is as follows:
>
> class MyConsumer(JsonWebsocketConsumer):
> def connect(self):
> self.accept()
> if not authenticated:
> self.close(4001)
>
>
> The Channels documentation is ambiguous as to which of these is correct.
> Now I think I know the answer as the first way breaks and the second way
> works, but I wanted to check with someone more knowledgeable that this way
> of doing things is by design and not just working by accident.  It feels a
> bit wrong to accept a connection and then immediately close it, but I
> understand if you have to do it this way, to get the custom close code
> returned perhaps you have to have an open connection in the first place.
> Can someone please confirm?
>
> Many thanks!
> Matt
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/6a4a797c-e590-4f4d-a718-074a3cf704bc%40googlegroups.com
> 
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAFwN1urect58FqfPUJExwbWd%2BpyWTjU4gLwNdEq9Sm0d1Lv75w%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Deploying Django app with Channels on Linux Hosting

2018-10-03 Thread Andrew Godwin
Hi there - it sounds like your host does not support websockets. You should
check with them for more.

Andrew

On Wed, 3 Oct 2018 at 23:43, Manjunath  wrote:

> Hi all,
> I have developed a simple app using django channels.
>
> I would like to deploy it on a linux hosting provider (Currently I have
> account with mochahost.com - Web Hosting:mocha package) & I have deployed
> the app there.
> All features of the app are working except for web sockets which give the
> following error: *Error during WebSocket handshake: Unexpected response
> code: 404*
>
> The app in local development system works fine without a glitch but the
> ASGI layer is not working after deployment.
>
> My current host provider is running pyhon apps using  Passenger.
>
> Is there any extra settings I need to do to setup the Socket layer?
> Kindly help me in this issue.
>
> Package versions used:
> Python - 3.6.6
> Django - 2.1.1
> Channels - 2.1.3
> Channels_redis - 2.3.0
>
>
> Hope to get help for this issue..
>
> Thanks & Regards,
> Manjunath
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/e39d3fb8-bad0-4852-a3df-aa52ebf45d6d%40googlegroups.com
> 
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAFwN1uqPpNxOkY-E22vRQw%2B%3D2_iEQ0TJHqVuV-LOn-pMjRYmxA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: django ORM with asyncio

2018-09-18 Thread Andrew Godwin
The Django ORM can't just be used with asyncio directly, sadly. The
database_sync_to_async function you found there will help you call the ORM
from asynchronous code, but it still runs the ORM in a synchronous thread
(see https://channels.readthedocs.io/en/latest/topics/databases.html)

The project to make the ORM truly async is a tough one and yet to be
started.

Andrew

On Tue, Sep 18, 2018 at 5:55 AM luke lukes  wrote:

> I'm looking for something that can allow the usage of the Django ORM with
> asyncio (with PostgreSQL).
>
> As of now I found only aiopg  in the 
> *asyncio
> ecosystem* , which allows to run raw SQL or
> to use SQLAlchemy only.
>
>
> I then found something in the django channels docs
> 
> .
>
>
> is this:
>
> https://github.com/django/channels/blob/master/channels/db.py
>
>
> the missing piece that I was looking for?
>
>
> Thanks
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/2af17ca1-6d48-45b0-9406-37e046934567%40googlegroups.com
> 
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAFwN1uq7sZPzw1M6DJAqrFs7pttFz6PWemobgHogCsN6PR-jpQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Channels thread count is increasing

2018-09-17 Thread Andrew Godwin
There's a reasonably large threadpool that sync_to_async uses (I think it
defaults to number of CPU cores * 5), so you're probably just seeing that.
It should stop growing at some point.

Andrew

On Sat, Sep 15, 2018 at 4:02 PM erkin kabataş  wrote:

> Hi All,
>
> I am new to python and django.
> I am using Python 3.6.5, Django 2.1 and Channels 2.1.2.
> My problem is when I switch to ASGI from WSGI server, in every request,
> thread count is increasing. Same thing is happening when I use
> sync_to_async function in consumers.py. Is this a normal behaviour?
>
> Thanks,
> Erkin
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/8635d3c1-c1e6-43d2-a70c-3db2ef3cc1f3%40googlegroups.com
> 
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAFwN1upOyJw%2BDKZmF-zJ__Tnyx2X3cA-YrY%2B%3D1iRUzZc4Tq%3DTg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Channels/Redis fault tolerance with Twemproxy?

2018-08-05 Thread Andrew Godwin
Channels 1 and its Redis bindings have different behaviours to 2, so this
might be improved there (but I can't guarantee it).

Andrew

On Sun, Aug 5, 2018 at 11:48 AM Filbert  wrote:

> Wow, I thought I had this working using haproxy in front of
> redis-sentinel.  haproxy fails over when redis-sentinel fails over within
> 30 seconds.
>
> Problem is Daphne (Channels 1.X), reports over and over:
>
> Error trying to receive messages: Error running script (call to
> f_3640886a0c8901ca9188f5f7a5f7a346145b9c5f): @user_script:3: @user_script:
> 3: -READONLY You can't write against a read only slave.
>
>
> Which I assume means Daphne doesn't detect a problem and just keeps the
> socket open on the (old) master that is now the slave.
>
>
> On Thursday, August 2, 2018 at 8:53:24 PM UTC-4, Andrew Godwin wrote:
>>
>> Yup, any sort of TCP-level proxy with failover will work for this sort of
>> thing, but you'll likely see some errors whenever you failover as Channels
>> loses its blocking connection that it's using to wait for messages - as
>> always, please test a failover before you rely on it!
>>
>> Andrew
>>
>> On Thu, Aug 2, 2018 at 5:50 PM Filbert  wrote:
>>
>>> Andrew,
>>> Just to be clear, there is no reason I can't use haproxy to front end
>>> redis sentinel to effectively hide all this from channels 1 or 2 right?
>>> Very surprised there isn't anyone running this in production that doesn't
>>> need some form of HA.  Buried trying to get a product out the door right
>>> now, but as I get time and more familiar with channels I'll probably dig in
>>> deeper on this.
>>> Thanks.
>>>
>>> On Thursday, August 2, 2018 at 7:32:52 PM UTC-4, Andrew Godwin wrote:
>>>>
>>>> The Sentinel option was removed in the 2.0 rewrite as it didn't have
>>>> someone to help maintain it (I didn't have the time then and don't right
>>>> now), so you are right, there's currently no easy HA option for Redis with
>>>> Channels.
>>>>
>>>> If you're interested in one, I can give you some tips on what code
>>>> needs to be written, but it's not something I really want to maintain
>>>> myself as part of the greater Channels effort as HA solutions tend to be
>>>> quite workload-heavy to develop and test and end up being specific to
>>>> certain deployment scenarios. Contributions of code that could help are
>>>> welcome, though.
>>>>
>>>> Andrew
>>>>
>>>> On Wed, Aug 1, 2018 at 1:22 PM Filbert  wrote:
>>>>
>>>>> Well in digging through posts I see Sentinel may be an option, does
>>>>> that mean if my CHANNEL_LAYER would have a single host entry?
>>>>>
>>>>> On Wednesday, August 1, 2018 at 9:42:39 AM UTC-4, Filbert wrote:
>>>>>>
>>>>>> Thanks Andrew,
>>>>>> So with a load-balanced ten web server implementation using Channels
>>>>>> (and of course ten instances of redis) do I have a HA solution then?  
>>>>>> RIght
>>>>>> now Django settings simply has a list of those servers in CHANNEL_LAYERS,
>>>>>> what if (say) three of them crash, will Channels continue to try and hash
>>>>>> to the dead servers? Again, I'll accept a brief set of errors, but if 
>>>>>> this
>>>>>> effectively causes Channels to continue to error that is not my 
>>>>>> definition
>>>>>> of HA.
>>>>>> Thanks.
>>>>>>
>>>>>> On Sunday, July 22, 2018 at 8:09:16 PM UTC-4, Andrew Godwin wrote:
>>>>>>>
>>>>>>> I don't think redis-cluster supports BLPOP properly still so I don't
>>>>>>> think you can use it.
>>>>>>>
>>>>>>> Andrew
>>>>>>>
>>>>>>> On Sun, Jul 22, 2018 at 12:46 PM Filbert  wrote:
>>>>>>>
>>>>>>>> And just to be clear as to my options, using Channels 2.0 I can use
>>>>>>>> Redis cluster if I have to to get failover and fault tolerance, 
>>>>>>>> correct?
>>>>>>>>
>>>>>>>> On Sunday, July 22, 2018 at 12:57:22 PM UTC-4, Andrew Godwin wrote:
>>>>>>>>>
>>>>>>>>> I'm afraid I've never used Twemproxy, so I can't really comment on
>>>>>>>>> it, but if you don't care about data loss, 

Re: Channels/Redis fault tolerance with Twemproxy?

2018-08-02 Thread Andrew Godwin
Yup, any sort of TCP-level proxy with failover will work for this sort of
thing, but you'll likely see some errors whenever you failover as Channels
loses its blocking connection that it's using to wait for messages - as
always, please test a failover before you rely on it!

Andrew

On Thu, Aug 2, 2018 at 5:50 PM Filbert  wrote:

> Andrew,
> Just to be clear, there is no reason I can't use haproxy to front end
> redis sentinel to effectively hide all this from channels 1 or 2 right?
> Very surprised there isn't anyone running this in production that doesn't
> need some form of HA.  Buried trying to get a product out the door right
> now, but as I get time and more familiar with channels I'll probably dig in
> deeper on this.
> Thanks.
>
> On Thursday, August 2, 2018 at 7:32:52 PM UTC-4, Andrew Godwin wrote:
>>
>> The Sentinel option was removed in the 2.0 rewrite as it didn't have
>> someone to help maintain it (I didn't have the time then and don't right
>> now), so you are right, there's currently no easy HA option for Redis with
>> Channels.
>>
>> If you're interested in one, I can give you some tips on what code needs
>> to be written, but it's not something I really want to maintain myself as
>> part of the greater Channels effort as HA solutions tend to be quite
>> workload-heavy to develop and test and end up being specific to certain
>> deployment scenarios. Contributions of code that could help are welcome,
>> though.
>>
>> Andrew
>>
>> On Wed, Aug 1, 2018 at 1:22 PM Filbert  wrote:
>>
>>> Well in digging through posts I see Sentinel may be an option, does that
>>> mean if my CHANNEL_LAYER would have a single host entry?
>>>
>>> On Wednesday, August 1, 2018 at 9:42:39 AM UTC-4, Filbert wrote:
>>>>
>>>> Thanks Andrew,
>>>> So with a load-balanced ten web server implementation using Channels
>>>> (and of course ten instances of redis) do I have a HA solution then?  RIght
>>>> now Django settings simply has a list of those servers in CHANNEL_LAYERS,
>>>> what if (say) three of them crash, will Channels continue to try and hash
>>>> to the dead servers? Again, I'll accept a brief set of errors, but if this
>>>> effectively causes Channels to continue to error that is not my definition
>>>> of HA.
>>>> Thanks.
>>>>
>>>> On Sunday, July 22, 2018 at 8:09:16 PM UTC-4, Andrew Godwin wrote:
>>>>>
>>>>> I don't think redis-cluster supports BLPOP properly still so I don't
>>>>> think you can use it.
>>>>>
>>>>> Andrew
>>>>>
>>>>> On Sun, Jul 22, 2018 at 12:46 PM Filbert  wrote:
>>>>>
>>>>>> And just to be clear as to my options, using Channels 2.0 I can use
>>>>>> Redis cluster if I have to to get failover and fault tolerance, correct?
>>>>>>
>>>>>> On Sunday, July 22, 2018 at 12:57:22 PM UTC-4, Andrew Godwin wrote:
>>>>>>>
>>>>>>> I'm afraid I've never used Twemproxy, so I can't really comment on
>>>>>>> it, but if you don't care about data loss, it's probably fine?
>>>>>>>
>>>>>>> Andrew
>>>>>>>
>>>>>>> On Fri, Jul 20, 2018 at 3:30 PM Filbert  wrote:
>>>>>>>
>>>>>>>> Going to have 10 or more EC2 instances running Channels 2.0.  I
>>>>>>>> really don't want to add the complexity of Redis Cluster or Redis
>>>>>>>> sentinel.  Is a decent fault tolerant solution to implement Twemproxy,
>>>>>>>> instead.  I don't care about data loss if an instance crashes, but I do
>>>>>>>> care that the application continues to operate on the remaining 
>>>>>>>> instances.
>>>>>>>>
>>>>>>>> Is this a workable solution?
>>>>>>>>
>>>>>>>> --
>>>>>>>> You received this message because you are subscribed to the Google
>>>>>>>> Groups "Django users" group.
>>>>>>>> To unsubscribe from this group and stop receiving emails from it,
>>>>>>>> send an email to django-users...@googlegroups.com.
>>>>>>>> To post to this group, send email to django...@googlegroups.com.
>>>>>>>> Visit this group at https://groups.google.com/group/django-users.
>>>>>>&g

Re: Channels/Redis fault tolerance with Twemproxy?

2018-08-02 Thread Andrew Godwin
The Sentinel option was removed in the 2.0 rewrite as it didn't have
someone to help maintain it (I didn't have the time then and don't right
now), so you are right, there's currently no easy HA option for Redis with
Channels.

If you're interested in one, I can give you some tips on what code needs to
be written, but it's not something I really want to maintain myself as part
of the greater Channels effort as HA solutions tend to be quite
workload-heavy to develop and test and end up being specific to certain
deployment scenarios. Contributions of code that could help are welcome,
though.

Andrew

On Wed, Aug 1, 2018 at 1:22 PM Filbert  wrote:

> Well in digging through posts I see Sentinel may be an option, does that
> mean if my CHANNEL_LAYER would have a single host entry?
>
> On Wednesday, August 1, 2018 at 9:42:39 AM UTC-4, Filbert wrote:
>>
>> Thanks Andrew,
>> So with a load-balanced ten web server implementation using Channels (and
>> of course ten instances of redis) do I have a HA solution then?  RIght now
>> Django settings simply has a list of those servers in CHANNEL_LAYERS, what
>> if (say) three of them crash, will Channels continue to try and hash to the
>> dead servers? Again, I'll accept a brief set of errors, but if this
>> effectively causes Channels to continue to error that is not my definition
>> of HA.
>> Thanks.
>>
>> On Sunday, July 22, 2018 at 8:09:16 PM UTC-4, Andrew Godwin wrote:
>>>
>>> I don't think redis-cluster supports BLPOP properly still so I don't
>>> think you can use it.
>>>
>>> Andrew
>>>
>>> On Sun, Jul 22, 2018 at 12:46 PM Filbert  wrote:
>>>
>>>> And just to be clear as to my options, using Channels 2.0 I can use
>>>> Redis cluster if I have to to get failover and fault tolerance, correct?
>>>>
>>>> On Sunday, July 22, 2018 at 12:57:22 PM UTC-4, Andrew Godwin wrote:
>>>>>
>>>>> I'm afraid I've never used Twemproxy, so I can't really comment on it,
>>>>> but if you don't care about data loss, it's probably fine?
>>>>>
>>>>> Andrew
>>>>>
>>>>> On Fri, Jul 20, 2018 at 3:30 PM Filbert  wrote:
>>>>>
>>>>>> Going to have 10 or more EC2 instances running Channels 2.0.  I
>>>>>> really don't want to add the complexity of Redis Cluster or Redis
>>>>>> sentinel.  Is a decent fault tolerant solution to implement Twemproxy,
>>>>>> instead.  I don't care about data loss if an instance crashes, but I do
>>>>>> care that the application continues to operate on the remaining 
>>>>>> instances.
>>>>>>
>>>>>> Is this a workable solution?
>>>>>>
>>>>>> --
>>>>>> You received this message because you are subscribed to the Google
>>>>>> Groups "Django users" group.
>>>>>> To unsubscribe from this group and stop receiving emails from it,
>>>>>> send an email to django-users...@googlegroups.com.
>>>>>> To post to this group, send email to django...@googlegroups.com.
>>>>>> Visit this group at https://groups.google.com/group/django-users.
>>>>>> To view this discussion on the web visit
>>>>>> https://groups.google.com/d/msgid/django-users/d1d50730-c1ba-4674-8ef8-54294158d0f6%40googlegroups.com
>>>>>> <https://groups.google.com/d/msgid/django-users/d1d50730-c1ba-4674-8ef8-54294158d0f6%40googlegroups.com?utm_medium=email_source=footer>
>>>>>> .
>>>>>> For more options, visit https://groups.google.com/d/optout.
>>>>>>
>>>>> --
>>>> You received this message because you are subscribed to the Google
>>>> Groups "Django users" group.
>>>> To unsubscribe from this group and stop receiving emails from it, send
>>>> an email to django-users...@googlegroups.com.
>>>> To post to this group, send email to django...@googlegroups.com.
>>>> Visit this group at https://groups.google.com/group/django-users.
>>>> To view this discussion on the web visit
>>>> https://groups.google.com/d/msgid/django-users/42fe264e-7f1f-426a-82c6-36aed944a923%40googlegroups.com
>>>> <https://groups.google.com/d/msgid/django-users/42fe264e-7f1f-426a-82c6-36aed944a923%40googlegroups.com?utm_medium=email_source=footer>
>>>> .
>>>> For more options, visit https://groups.google.com/d/optout.
>>>>
>>> --
> You 

Re: Problem with sending django channels event from models

2018-07-31 Thread Andrew Godwin
Yes, you can't just call "group_send" without awaiting it, or you'll get
those warnings. You need async_to_sync, I'm afraid. Otherwise, you're not
actually waiting for the group_send function to finish and you'll start
getting some unreliable test results.

Andrew

On Tue, Jul 31, 2018 at 6:14 AM luan fonceca  wrote:

> Hello Andrew, sorry for the lack of traceback on the gist. I have changed
> my code a bit, i was using the `async_to_sync` but now i'm using this way
> inside my User save method:
>
> channel_layer = get_channel_layer()
> channel_layer.group_send(
> 'users_{}'.format(self.pk), {
> 'type': 'users.post_save',
> 'user_pk': self.pk
> }
> )
>
> And everything works fine, but my tests are raising warnings on each test,
> like this, if you want to see the full traceback of my test, click here
> <https://gist.github.com/luanfonceca/dafecba067d4caa8bfcf22d683409531#file-traceback>
> :
>
> users/tests/test_consumers.py::test_user_consumer
>   /Users/luan/Sources/channels-test/users/models.py:56: RuntimeWarning:
> coroutine 'RedisChannelLayer.group_send' was never awaited
> 'user_pk': self.pk
>
> I have update my gist, if you want to see something else, but in general
> i'm using py.test, as you can see in here
> <https://gist.github.com/luanfonceca/dafecba067d4caa8bfcf22d683409531#file-test-py>
> .
>
> https://gist.github.com/luanfonceca/dafecba067d4caa8bfcf22d683409531
>
>
> Thanks for everything, Andrew.
>
> Em sex, 27 de jul de 2018 às 19:30, Andrew Godwin 
> escreveu:
>
>> You need to post the full traceback, not just the last section. I suspect
>> what you are doing is calling model methods directly in an async function,
>> but I can't confirm that without a full traceback.
>>
>> Andrew
>>
>> On Thu, Jul 26, 2018 at 1:10 PM luan fonceca 
>> wrote:
>>
>>> Hello, i'm trying to implement a "post_save" signal wherever some user
>>> are update. I setup everything by following the documentation and some
>>> tutorials for Channels 2. I wrote a test using "py.test" to assert that
>>> everything is working as expected but i'm getting a exception
>>> <https://gist.github.com/luanfonceca/dafecba067d4caa8bfcf22d683409531#file-traceback>
>>> .
>>>
>>> On this link i have posted a gist.
>>> <https://gist.github.com/luanfonceca/dafecba067d4caa8bfcf22d683409531#file-traceback>
>>>
>>> Thanks in advance.
>>>
>>> --
>>>
>> You received this message because you are subscribed to the Google Groups
>>> "Django users" group.
>>> To unsubscribe from this group and stop receiving emails from it, send
>>> an email to django-users+unsubscr...@googlegroups.com.
>>
>>
>>> To post to this group, send email to django-users@googlegroups.com.
>>> Visit this group at https://groups.google.com/group/django-users.
>>> To view this discussion on the web visit
>>> https://groups.google.com/d/msgid/django-users/3f42ad5c-c737-4a5e-baad-54023261d7f5%40googlegroups.com
>>> <https://groups.google.com/d/msgid/django-users/3f42ad5c-c737-4a5e-baad-54023261d7f5%40googlegroups.com?utm_medium=email_source=footer>
>>> .
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>> --
>> You received this message because you are subscribed to a topic in the
>> Google Groups "Django users" group.
>> To unsubscribe from this topic, visit
>> https://groups.google.com/d/topic/django-users/3hZUiOvhSfE/unsubscribe.
>> To unsubscribe from this group and all its topics, send an email to
>> django-users+unsubscr...@googlegroups.com.
>> To post to this group, send email to django-users@googlegroups.com.
>> Visit this group at https://groups.google.com/group/django-users.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/django-users/CAFwN1uomro%2B1-qkGC8ThJw6ehz%3DHTenXwUqx-xM%3DkzzTqNgQdg%40mail.gmail.com
>> <https://groups.google.com/d/msgid/django-users/CAFwN1uomro%2B1-qkGC8ThJw6ehz%3DHTenXwUqx-xM%3DkzzTqNgQdg%40mail.gmail.com?utm_medium=email_source=footer>
>> .
>> For more options, visit https://groups.google.com/d/optout.
>>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/

Re: Problem with sending django channels event from models

2018-07-27 Thread Andrew Godwin
You need to post the full traceback, not just the last section. I suspect
what you are doing is calling model methods directly in an async function,
but I can't confirm that without a full traceback.

Andrew

On Thu, Jul 26, 2018 at 1:10 PM luan fonceca  wrote:

> Hello, i'm trying to implement a "post_save" signal wherever some user are
> update. I setup everything by following the documentation and some
> tutorials for Channels 2. I wrote a test using "py.test" to assert that
> everything is working as expected but i'm getting a exception
> 
> .
>
> On this link i have posted a gist.
> 
>
> Thanks in advance.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/3f42ad5c-c737-4a5e-baad-54023261d7f5%40googlegroups.com
> 
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAFwN1uomro%2B1-qkGC8ThJw6ehz%3DHTenXwUqx-xM%3DkzzTqNgQdg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: User Authenticated in Gunicorn and Daphne

2018-07-27 Thread Andrew Godwin
You can configure Nginx to route to different backends based on path - put
your Nginx proxy_pass settings inside a "location" block.

You can find examples on Stack Overflow - for example:
https://stackoverflow.com/questions/13399252/nginx-reverse-proxy-to-multiple-backends

Andrew

On Tue, Jul 24, 2018 at 1:09 PM  wrote:

> Hi there,
>
> ideally I would like to run Daphne alongside Gunicorn and not Daphne alone:
>
> - My chat with Daphne
> - Rest of the website with Gunicorn
>
> A user logs in - request via Gunicorn. Now when he connects with the chat
> I get an AnonymousUser using self.scope['user']. This is a problem because
> I would like to assign him to the right ChatRoom.
> If I run all requests through Daphne everything works fine.
>
> Also I am using Nginx, don't know if it matters, just gonna leave it here.
>
> How could I fix this? What would be the workaround?
>
> Let me know if you need any code/files.
>
> Best Regards,
>
> Dario
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/cb2a7475-69dd-4a09-ba32-ea89950d5fc7%40googlegroups.com
> 
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAFwN1uo8w8jSe2bALJKfBwzmmLN9VtJwcARBGRhzc%3D%3DQy%2B2AdA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Connecting to remote host like Twitch to monitor chat using websockets

2018-07-24 Thread Andrew Godwin
Hi Dave,

It depends on what the protocol for the chat room is, but Channels is more
for hosting websocket-based applications than consuming them. I suspect you
would be better off looking at websocket client libraries, if the chat
socket is even accessible.

Andrew

On Mon, Jul 23, 2018 at 4:45 PM Dave Holly  wrote:

>
> Greetings,
>
> Is it possible to use Django Channels and websockets to monitor the chat
> in twitch rooms, or other sites with chat rooms?  I would like to display
> extra graphics for users who send cheers of high amounts, like 1000, 2000,
> etc.  I have the display worked out in Django where I'm monitoring a simple
> local chat.  This would be used as an overlay web page for OBS for the
> broadcaster.  When someone posts a specific cheer amount, the javascript
> displays a message or gif on the overlay web page with a short timeout.
> It's basically to say Thank You to the tipper.
>
> I haven't found any documentation or tutorials on using Django Channels
> for anything but setting up my own chat program.
>
> In the consumers, is there a way to connect to a site like twitch and
> search the DOM for specific chat messages, like cheers?
>
>
> Thanks.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/79ddc763-57d5-4757-9026-f0960b06c5a3%40googlegroups.com
> 
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAFwN1uqfVt4CSsJmV0bXjBobzZ4BNV%3DdaqKY7jaRHPEpHXCAzQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Channels/Redis fault tolerance with Twemproxy?

2018-07-22 Thread Andrew Godwin
I don't think redis-cluster supports BLPOP properly still so I don't think
you can use it.

Andrew

On Sun, Jul 22, 2018 at 12:46 PM Filbert  wrote:

> And just to be clear as to my options, using Channels 2.0 I can use Redis
> cluster if I have to to get failover and fault tolerance, correct?
>
> On Sunday, July 22, 2018 at 12:57:22 PM UTC-4, Andrew Godwin wrote:
>>
>> I'm afraid I've never used Twemproxy, so I can't really comment on it,
>> but if you don't care about data loss, it's probably fine?
>>
>> Andrew
>>
>> On Fri, Jul 20, 2018 at 3:30 PM Filbert  wrote:
>>
>>> Going to have 10 or more EC2 instances running Channels 2.0.  I really
>>> don't want to add the complexity of Redis Cluster or Redis sentinel.  Is a
>>> decent fault tolerant solution to implement Twemproxy, instead.  I don't
>>> care about data loss if an instance crashes, but I do care that the
>>> application continues to operate on the remaining instances.
>>>
>>> Is this a workable solution?
>>>
>>> --
>>> You received this message because you are subscribed to the Google
>>> Groups "Django users" group.
>>> To unsubscribe from this group and stop receiving emails from it, send
>>> an email to django-users...@googlegroups.com.
>>> To post to this group, send email to django...@googlegroups.com.
>>> Visit this group at https://groups.google.com/group/django-users.
>>> To view this discussion on the web visit
>>> https://groups.google.com/d/msgid/django-users/d1d50730-c1ba-4674-8ef8-54294158d0f6%40googlegroups.com
>>> <https://groups.google.com/d/msgid/django-users/d1d50730-c1ba-4674-8ef8-54294158d0f6%40googlegroups.com?utm_medium=email_source=footer>
>>> .
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/42fe264e-7f1f-426a-82c6-36aed944a923%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/42fe264e-7f1f-426a-82c6-36aed944a923%40googlegroups.com?utm_medium=email_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAFwN1uq5bDK%2BBcAOg4NsGTJjBi%3DmDCqqhOZWXSExvm7RtdzN7w%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Channels/Redis fault tolerance with Twemproxy?

2018-07-22 Thread Andrew Godwin
I'm afraid I've never used Twemproxy, so I can't really comment on it, but
if you don't care about data loss, it's probably fine?

Andrew

On Fri, Jul 20, 2018 at 3:30 PM Filbert  wrote:

> Going to have 10 or more EC2 instances running Channels 2.0.  I really
> don't want to add the complexity of Redis Cluster or Redis sentinel.  Is a
> decent fault tolerant solution to implement Twemproxy, instead.  I don't
> care about data loss if an instance crashes, but I do care that the
> application continues to operate on the remaining instances.
>
> Is this a workable solution?
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/d1d50730-c1ba-4674-8ef8-54294158d0f6%40googlegroups.com
> 
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAFwN1upoXL-Kueo6BS45wOYkW9ULKMdk%2BSQOgAWVx20QunKVBQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: How to check if server is running in ASGI or in WSGI mode?

2018-07-22 Thread Andrew Godwin
Where do you want to detect this? There's no official way of knowing,
because you're meant to detect based on features/what gets called.

Andrew

On Thu, Jul 19, 2018 at 2:12 PM Rafis Ganeev  wrote:

> We are running same django project in WSGI mode for handling HTTP requests
> and in ASGI mode for handling WebSockets. For WSGI mode we are using
> **gunicorn3** server:
>
> gunicorn3 --pythonpath . -b 0.0.0.0:8000 chat_bot.wsgi:application
>
> For ASGI mode we are using **daphne** server:
>
> daphne --root-path . -b 0.0.0.0 -p 8001 chat_bot.asgi:application
>
> How to programatically detect which mode currently is running
> **GUnicorn+WSGI** or **Daphne+ASGI**?
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/4ec22aa6-e69e-4cb6-8463-d3a9eef4728e%40googlegroups.com
> 
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAFwN1urgCsAudE2LLea9MEQGPFeHX0qP%3DzHYfvA%2ByVHycJ9xLg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: [Channels] How to write tests with database access?

2018-07-22 Thread Andrew Godwin
Asynchronous testing is very hard, unfortunately. I suspect what you are
doing is awaiting the sending of the message, but not awaiting any response
back, so your test continues once it's sent the message into the
application but not waiting for that application to finish processing.

The best pattern for this is probably to send something back when you've
finished processing and await receiving that, then you should guarantee
that the work has been finished before your test continues.

Andrew

On Thu, Jul 19, 2018 at 8:18 AM Neraste  wrote:

> Hello Django users,
>
> This sounds like a pretty newbish question, but I am struggling on how to
> write tests for Django-channels 2 websocket consumers that access the
> database. I am not familiar with pytest and asynchronous programming (the
> bread and butter of Channels testing), so while I caught up with pytest, I
> am still a bit lost between `async` and `await`.
>
> Using Django/Django-REST, when I test a view method which modifies a
> database object for a given request, here is what I usually do:
>
>1. Assert the initial state of the object ;
>2. Use the test client to send the request ;
>3. Assert the outcome of the request ;
>4. Assert the final state of the object, which should be different.
>
> All of this synchronously. With Channels, I thought I could do the same
> for a consumer method which modifies a database object on a given event:
>
>1. Assert the initial state of the object ;
>2. Use the test communicator to send the event ;
>3. Assert the final state of the object, which should have changed.
>
> All of this asynchronously, as stated in the documentation
> , but this
> is a synchronous test scenario! Obviously, it does not work, as step 2. is
> executed with `await` and step 3. is achieved before step 2. finishes. I
> tried to execute step 2. synchronously using `async_to_sync` but it does
> not work (I can give details).
>
> I am missing something, how can I test such a consumer method? Is this
> possible? The documentation does not give any hint on this…
>
> Help would be much appreciated!
>
> Cheers,
>
> --
> Neraste
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/c22c1afb-ff85-5af9-66f6-a94cdc77a545%40gmail.com
> 
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAFwN1upvEzDpa4XWGunLVpfM%3Dj%2B5%3DH7sBUDtYOe8Q57hioP%3DLw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Django Channels: long running async function and where to save ORM object

2018-07-18 Thread Andrew Godwin
You can use async inside if you want, it's your choice. You will always
have to run the separate process, though, yes. That's just a cleaner way of
doing background tasks.

Andrew

On Mon, Jul 16, 2018 at 11:20 AM Sebastian Haase 
wrote:

> Thanks for the reply.   Should I still use async code inside that worker
> process ?
> While developing I simply do arun_serveron the command line  --
> then I would always have to do arun_worker   in addition  -- right ?
>
> -Sebastian
>
>
> On Mon, Jul 16, 2018 at 7:05 PM, Andrew Godwin 
> wrote:
>
>> You are correct in that you could store the model instance on "self" but
>> this would result in caching issues, as that instance would not be updated
>> after the initial socket connection.
>>
>> If you want to run "background routines", I would discourage you from
>> doing these as random orphaned tasks launched from web processes - there's
>> no provision in the server to track that task and clean up after it.
>> Instead, I'd recommend running those kinds of tasks in a separate process.
>>
>> Andrew
>>
>> On Sun, Jul 15, 2018 at 2:03 AM Sebastian Haase 
>> wrote:
>>
>>> Hi,
>>> I'm still in the process of migration from channels 1 to channels 2...
>>>
>>> One thing I'm interested in is related to [
>>> http://channels.readthedocs.io/en/latest/one-to-two.html#application-instances
>>> ]
>>> """ASGI applications are now instantiated once per socket and can use
>>> local variables on self to store information"""
>>>
>>> So, we have an `AsyncJsonWebsocketConsumer` where websocket clients want
>>> to operate on a django model instance - each websocket mostly on the same
>>> object.
>>> With channels 1 we would always send with each message the `objID` -
>>> doing `obj=model.objects.get(pk=objID)` and read/write on `obj`.
>>> In channels 2 I could save the `objID` server side in the consumer class
>>> - like `self.objID` with one initial message like `set-obj-id`.
>>> For the ORM access I now have to do `obj = await database_sync_to_async(
>>> model.objects.get )(pk=self.objID)`
>>>
>>> QUESTION: Is is legitimate to store the "model instance itself" like
>>> `self.obj = await database_sync_to_async( model.objects.get )(pk=objID)`
>>> and keep that instance around for the lifetime of that socket -- doing some
>>> `await database_sync_to_async( self.obj.save )()` on some respective
>>> messages ?
>>> Note:  multiple websockets might operate on the same obj/objID ! So my
>>> feeling is that  would work as long as I keep testing with the `runserver`
>>> or in production as long as I know that I have only ONE PROCESS -- but in
>>> general the django caching mechanism might cause inconsistencies ... !?
>>>
>>>
>>> Another, somewhat related question is regarding a kind of "background
>>> routine"  that I started to implement as an asyncio Task.
>>> Note:  It was kind of tricky to realize that I needed
>>> `asyncio.ensure_future` to get it run asynchronously -- ref.
>>> https://stackoverflow.com/questions/48871831/django-channels-async-consumer-does-not-appear-to-execute-asynchronously
>>> Maybe that could be worked into the channels docs maybe here:
>>> http://channels.readthedocs.io/en/latest/topics/worker.html
>>> Since that "routine" should be able to be canceled and pauses (I'm using
>>> an asyncio.event) from any websocket consumer:
>>> QUESTION: What is the  best place to store that Task object ?  Again
>>> with `runserver` I can just use a global module-level `dict` to keep those
>>> Task objects.
>>>
>>> The whole async experience is really twisting my mind  and interesting
>>> at the same time ;-)
>>> Regards,
>>> Sebastian
>>>
>>>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAN06oV-ySRN%3D-9BEKd8SfvgB52F3QagNMh_rZqQQ6wCS3HFEkg%40mail.gmail.com
> <https://groups.google.com/d/msgid/django-users/CAN06oV-ySRN%3D-9BEKd8SfvgB52F3QagNMh_rZqQQ6wCS3HFEkg%40mail.gmail.com?utm_medium=email_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAFwN1upuQALNkKO7engdybnovH2%2BCkR7CE%2BMo1k2yFkwLkk7oA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Django Channels: long running async function and where to save ORM object

2018-07-16 Thread Andrew Godwin
You are correct in that you could store the model instance on "self" but
this would result in caching issues, as that instance would not be updated
after the initial socket connection.

If you want to run "background routines", I would discourage you from doing
these as random orphaned tasks launched from web processes - there's no
provision in the server to track that task and clean up after it. Instead,
I'd recommend running those kinds of tasks in a separate process.

Andrew

On Sun, Jul 15, 2018 at 2:03 AM Sebastian Haase  wrote:

> Hi,
> I'm still in the process of migration from channels 1 to channels 2...
>
> One thing I'm interested in is related to [
> http://channels.readthedocs.io/en/latest/one-to-two.html#application-instances
> ]
> """ASGI applications are now instantiated once per socket and can use
> local variables on self to store information"""
>
> So, we have an `AsyncJsonWebsocketConsumer` where websocket clients want
> to operate on a django model instance - each websocket mostly on the same
> object.
> With channels 1 we would always send with each message the `objID` - doing
> `obj=model.objects.get(pk=objID)` and read/write on `obj`.
> In channels 2 I could save the `objID` server side in the consumer class -
> like `self.objID` with one initial message like `set-obj-id`.
> For the ORM access I now have to do `obj = await database_sync_to_async(
> model.objects.get )(pk=self.objID)`
>
> QUESTION: Is is legitimate to store the "model instance itself" like
> `self.obj = await database_sync_to_async( model.objects.get )(pk=objID)`
> and keep that instance around for the lifetime of that socket -- doing some
> `await database_sync_to_async( self.obj.save )()` on some respective
> messages ?
> Note:  multiple websockets might operate on the same obj/objID ! So my
> feeling is that  would work as long as I keep testing with the `runserver`
> or in production as long as I know that I have only ONE PROCESS -- but in
> general the django caching mechanism might cause inconsistencies ... !?
>
>
> Another, somewhat related question is regarding a kind of "background
> routine"  that I started to implement as an asyncio Task.
> Note:  It was kind of tricky to realize that I needed
> `asyncio.ensure_future` to get it run asynchronously -- ref.
> https://stackoverflow.com/questions/48871831/django-channels-async-consumer-does-not-appear-to-execute-asynchronously
> Maybe that could be worked into the channels docs maybe here:
> http://channels.readthedocs.io/en/latest/topics/worker.html
> Since that "routine" should be able to be canceled and pauses (I'm using
> an asyncio.event) from any websocket consumer:
> QUESTION: What is the  best place to store that Task object ?  Again with
> `runserver` I can just use a global module-level `dict` to keep those Task
> objects.
>
> The whole async experience is really twisting my mind  and interesting at
> the same time ;-)
> Regards,
> Sebastian
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/123c14e2-65c6-499e-9a27-b3a256aab099%40googlegroups.com
> 
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAFwN1urRx-NWDRY2yE3PdNbzoXdua2KtK3DRj_c9-mqiu1g5eA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: [django-channels client wrapper] specifying no reconnection attempts

2018-07-15 Thread Andrew Godwin
I don't know much about that JavaScript code, I'm afraid, otherwise I would
try and help (it's likely we're going to deprecate support for it soon as
there's no maintainer for it right now anyway).

Take a look through the source code and see if there's something you can
call to stop reconnection attempts when you get a good close event, maybe?

Andrew

On Sat, Jul 14, 2018 at 5:31 AM bit bouncer  wrote:

> Basically, I don't want the websocket to automatically attempt
> reconnections after it has been closed by the server.
>
> I've tried
> new WebSocketBridge({maxReconnectAttempts: 1})
>
> and
> const socket = new WebSocketBridge({maxReconnectAttempts: 1})
>
> socket.connect('/ws/', undefined, {maxReconnectAttempts: 1})
>
> but to no avail.
>
> Am I doing something wrong? Any help would be appreciated.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/e4471747-3133-49b7-b98d-cf7ecb64c1db%40googlegroups.com
> 
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAFwN1uqdcj1fJ%2B9DUDXtgZ12%3D_RdCwFFBmCTgbGTMSvdjjNhxQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: ERROR - server - Error trying to receive messages: name 'txredisapi' is not defined

2018-07-13 Thread Andrew Godwin
What version of Channels are you using? It looks like you're trying to use
Channels 1 without Twisted redis support installed - did you try installing
the package it's asking for?

Andrew

On Thu, Jul 12, 2018 at 4:22 AM Hank Chu  wrote:

> 2018-07-12 17:35:08,070 - INFO - worker - Listening on channels
>> http.request, websocket.connect, websocket.disconnect, websocket.receive
>> 2018-07-12 17:35:08,072 - INFO - server - HTTP/2 support not enabled
>> (install the http2 and tls Twisted extras)
>> 2018-07-12 17:35:08,075 - INFO - server - Using native Twisted mode on
>> channel layer
>> 2018-07-12 17:35:08,075 - INFO - server - Listening on endpoint
>> tcp:port=80:interface=10.144.1.123
>> 2018-07-12 17:35:08,077 - ERROR - server - Error trying to receive
>> messages: name 'txredisapi' is not defined
>> 2018-07-12 17:35:13,079 - ERROR - server - Error trying to receive
>> messages: name 'txredisapi' is not defined
>
>
> Can someone help me to fix this problem?
>
> this my django's setting.py
>
>> redis_host = '10.144.1.123'
>> CHANNEL_LAYERS = {
>> "default": {
>> "BACKEND": "asgi_redis.RedisChannelLayer",
>> "CONFIG": {
>> "hosts": [(redis_host, 6379)],
>> },
>>"ROUTING": "coverage_wabsys_py3.routing.channel_routing",
>> },
>> }
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/e3405fbd-1f5a-407b-a18e-138f53a2fe6e%40googlegroups.com
> 
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAFwN1ur9nrWPJKu_rRhR_Cxve%3DeqFA7pr2NtX9bz8VCw9DhP1Q%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: How to handle Video/Audio streaming using Django Channels?

2018-06-25 Thread Andrew Godwin
I mean, Channels isn't going to be some magical solution for that. A/V
streaming still requires someone to sit down and write protocol code and a
lot of real-time processing, and ultimately that's a lot more work than a
WebSocket library.

Andrew

On Mon, Jun 25, 2018 at 10:49 AM  wrote:

> Hi Andrew - With Channels 2, can we have real-time audio/video streaming?
> Thanks/Tirthankar.
>
> On Saturday, November 5, 2016 at 4:16:21 AM UTC+5:30, Andrew Godwin wrote:
>>
>> Hi Sandro,
>>
>> Real-time video streaming is pretty intensive, and WebRTC already has
>> some part of this covered, though from what I recall it's in a peer-to-peer
>> fashion. You certainly could marshal it into binary websocket frames, but
>> WebSocket is a non-ideal protocol for video delivery due to the way it's
>> designed.
>>
>> Really, to do something like this properly would require native WebRTC
>> support in Channels, and we don't have that (yet). I'd suggest looking into
>> other software solutions to relay video streams instead.
>>
>> Andrew
>>
>> On Fri, Nov 4, 2016 at 6:09 PM, Sandro Salles 
>> wrote:
>>
>>> Hey Andrew,
>>>
>>> In fact i'm trying to implement a real-time video chat application,
>>> using react native on a mobile app (iOS).
>>>
>>> The app will capture the users video using a module
>>> called react-native-webrtc, and send the live stream via websocket to a
>>> Django application (using Django Channels) that will then stream that video
>>> in real time to all clients that are connect to this video channel.
>>>
>>> The problem is that i don't know how to handle the incoming video/audio
>>> stream. All the examples i found for Django Channels are for text based
>>> chats and things like that... but nothing about the ability to handle media
>>> streams...
>>>
>>> I know i could use NodeJS on the server-side, socket.io and some others
>>> node modules, but i would really like to use Django to accomplish that.
>>>
>>> Do you think this is even possibile?
>>>
>>> Thanks again!
>>>
>>> *Sandro Salles*
>>> e-mail. san...@snippet.com.br
>>> telefone. (11) 99203-9014 (cel/whatsapp)
>>>
>>>
>>> On Fri, Nov 4, 2016 at 2:49 PM, Andrew Godwin 
>>> wrote:
>>>
>>>> Hi Sandro,
>>>>
>>>> Could you highlight what the problems you're facing are? Channels is
>>>> not particularly designed for streaming large files - you're better doing
>>>> that using a CDN or dedicated file streaming software - so I'd be curious
>>>> to know what the particular issues you're facing are.
>>>>
>>>> Andrew
>>>>
>>>> On Fri, Nov 4, 2016 at 5:31 PM, Sandro Salles 
>>>> wrote:
>>>>
>>>>> Hello Guys,
>>>>>
>>>>> I can't find any documentation on how to handle video/audio streaming
>>>>> using Django Channels.
>>>>>
>>>>>
>>>>> I've found an incomplete tutorial here:
>>>>> https://artandlogic.com/2016/06/django-channels-ground-part-1/
>>>>> https://artandlogic.com/2016/06/django-channels-ground-part-2/
>>>>>
>>>>>
>>>>> But as i said, it's incomplete...
>>>>>
>>>>>
>>>>> Can someone give-me a hand here? I really have absolutely no idea on
>>>>> how to implement this, although i think it's possible.
>>>>>
>>>>>
>>>>> Thanks in advance!
>>>>>
>>>>>
>>>>> --
>>>>> You received this message because you are subscribed to the Google
>>>>> Groups "Django users" group.
>>>>> To unsubscribe from this group and stop receiving emails from it, send
>>>>> an email to django-users...@googlegroups.com.
>>>>> To post to this group, send email to django...@googlegroups.com.
>>>>> Visit this group at https://groups.google.com/group/django-users.
>>>>> To view this discussion on the web visit
>>>>> https://groups.google.com/d/msgid/django-users/44eb8328-836c-4e52-843f-5e7ba5533b46%40googlegroups.com
>>>>> <https://groups.google.com/d/msgid/django-users/44eb8328-836c-4e52-843f-5e7ba5533b46%40googlegroups.com?utm_medium=email_source=footer>
>>>>> .
>>>>> For more options, visit https://groups.google.com/d/optout.
>>>>>
>>>

Re: Channels 2.0, How can I configure the channels 2.0 to use with the default django's views

2018-06-24 Thread Andrew Godwin
I'm not quite sure what you mean - Channels will still serve Django views
as well unless you override HTTP serving as well as WebSocket, and even
then, you can follow the URLRouter docs to keep it working fine:
http://channels.readthedocs.io/en/latest/topics/routing.html#urlrouter

Andrew

On Thu, Jun 21, 2018 at 4:46 PM Franklin Sarmiento 
wrote:

> Hi everyone!,
>
> I'm new with the channels topic, I'm learing the version 2.0, I have a
> doubt, How can I configure the channels 2.0 to use with the default
> django's views
>
> Thanks a lot
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/afd5ac18-7f8b-4973-a3d8-a5df2a382b5a%40googlegroups.com
> 
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAFwN1uqtW4DHxaVRfSf6i4Lmc%2BJoJvHOxKYpoqynpsWF5AdA_A%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Channels 2.0 close code and http response status

2018-06-24 Thread Andrew Godwin
I'm not quite sure - WebSockets do have browser differences. You could also
try adding a delay between the accept and the close to see if that changes
how they handle it.

Andrew

On Sun, 24 Jun 2018, 06:02 Kirill Tyushchakov, 
wrote:

> Hi, Andrew! Thanks a lot for your response.
> I've changed my consumer's connect method like this:
>
> def connect(self):
> user = self.scope.get('user')
> super(NotificationsConsumer, self).connect()
> if user.is_anonymous:
> self.close(code=4003)
>
>
>
> I've tested it in Chrome, Firefox, Safari and IE 11 and here is the result
> that I've got:
> Chrome - 1006
> IE 11 - 1005
> Firefox - 4003
> Safari - 1006
>
> It doesn't return 403 HTTP status code. Seems it works fine, but I think I
> need to test it more.
> So the expected close code appears only in Firefox. Should I consider it
> as the browser issue?
>
>
> Hi!
>>
>> Close code 1006 means that the connection was closed abruptly by the
>> other end (basically, the socket just got chopped off without an actual
>> close frame being sent).
>>
>> This is what happens when you close during the handshake, which is what
>> closing during connect() does - at that point, the connection is still HTTP
>> before the upgrade has finished, and so Channels sends a HTTP 403 response
>> code down instead. Looks like your browser interprets this as code 1006 for
>> a websocket.
>>
>> There's no way to change the HTTP response code sent when a handshake is
>> terminated at the moment - it's hardcoded (as you can see in Daphne's
>> serverReject method, here:
>> https://github.com/django/daphne/blob/master/daphne/ws_protocol.py#L200).
>>
>> Given that no browser I know of will actually tell you the HTTP response
>> code in this case, I don't think there's much value in letting it be
>> changed - you're always going to see an aborted WebSocket connection code
>> instead. If you want to provide more detail to the user, you can instead
>> accept the socket, letting the handshake finish, and then close it
>> immediately with a custom WebSocket close code (you can do all of that
>> inside connect, I think).
>>
>> Andrew
>>
>> On Thu, Jun 21, 2018 at 2:52 PM Kirill Tyushchakov 
>> wrote:
>>
>>> Hello everyone!
>>> I'm new in Django Channels. I'm using Channels 2.0 and I have few
>>> questions about it.
>>> I'm using JsonWebsocketConsumer class for my consumer and I wrote my
>>> definition of connect method like this:
>>>
>>> def connect(self):
>>> user = self.scope.get('user')
>>> if user.is_anonymous:
>>> super(NotificationsConsumer, self).connect()
>>> else:
>>> self.close(code=4003)
>>>
>>>
>>> On client side I'm using native JS Websocket.
>>> But when I try to connect to this socket as unauthorzied user I get
>>> code 1006 and HTTP response status code 403.
>>>
>>> My questions is:
>>> 1) How can I send custom close code to client?
>>> 2) Can I send another HTTP response status code? In my case 401
>>>
>>> --
>>> You received this message because you are subscribed to the Google
>>> Groups "Django users" group.
>>> To unsubscribe from this group and stop receiving emails from it, send
>>> an email to django-users...@googlegroups.com.
>>> To post to this group, send email to django...@googlegroups.com.
>>> Visit this group at https://groups.google.com/group/django-users.
>>> To view this discussion on the web visit
>>> https://groups.google.com/d/msgid/django-users/446d3be6-438b-4e59-ad4f-7841895f4fcb%40googlegroups.com
>>> 
>>> .
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/b758b6f7-91f0-43b8-8ea8-087c137598c4%40googlegroups.com
> 
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAFwN1upiYydgeTQvhF1-XMz97MFHDBJoBqcypGeHQLK_6%2BCn-A%40mail.gmail.com.
For more options, visit 

Re: Channels 2.0 close code and http response status

2018-06-23 Thread Andrew Godwin
Hi!

Close code 1006 means that the connection was closed abruptly by the other
end (basically, the socket just got chopped off without an actual close
frame being sent).

This is what happens when you close during the handshake, which is what
closing during connect() does - at that point, the connection is still HTTP
before the upgrade has finished, and so Channels sends a HTTP 403 response
code down instead. Looks like your browser interprets this as code 1006 for
a websocket.

There's no way to change the HTTP response code sent when a handshake is
terminated at the moment - it's hardcoded (as you can see in Daphne's
serverReject method, here:
https://github.com/django/daphne/blob/master/daphne/ws_protocol.py#L200).

Given that no browser I know of will actually tell you the HTTP response
code in this case, I don't think there's much value in letting it be
changed - you're always going to see an aborted WebSocket connection code
instead. If you want to provide more detail to the user, you can instead
accept the socket, letting the handshake finish, and then close it
immediately with a custom WebSocket close code (you can do all of that
inside connect, I think).

Andrew

On Thu, Jun 21, 2018 at 2:52 PM Kirill Tyushchakov 
wrote:

> Hello everyone!
> I'm new in Django Channels. I'm using Channels 2.0 and I have few
> questions about it.
> I'm using JsonWebsocketConsumer class for my consumer and I wrote my
> definition of connect method like this:
>
> def connect(self):
> user = self.scope.get('user')
> if user.is_anonymous:
> super(NotificationsConsumer, self).connect()
> else:
> self.close(code=4003)
>
>
> On client side I'm using native JS Websocket.
> But when I try to connect to this socket as unauthorzied user I get
> code 1006 and HTTP response status code 403.
>
> My questions is:
> 1) How can I send custom close code to client?
> 2) Can I send another HTTP response status code? In my case 401
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/446d3be6-438b-4e59-ad4f-7841895f4fcb%40googlegroups.com
> 
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAFwN1upPzKf65Ju8RhbhMO67eoSxg2q00VgrNu_HApv7XVJtxg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Django Channels 2.0 - How to init and persist/communicate with only one instance of background worker for every channel_layer.send?

2018-06-23 Thread Andrew Godwin
I'm not sure why that would happen - the code that chooses what application
instance to use is here:
https://github.com/django/asgiref/blob/master/asgiref/server.py (under
get_or_create_application_instance).

If you're always using a single channel name, which gets passed as scope_id
(see here: https://github.com/django/channels/blob/master/channels/worker.py),
it really should be returning the same instance. I recommend you poke
around in there and do some debugging to figure out what's going on.

Andrew

On Fri, Jun 22, 2018 at 9:46 AM itsnate_b  wrote:

> I am only using one channel name and my expectation is that only one
> instance of the background worker class should be initiated/used. Instead,
> I am seeing a new class being initiated for each call routed to the
> background worker class. This is unexpected...
>
>
> On Thursday, June 21, 2018 at 3:55:23 PM UTC-4, Andrew Godwin wrote:
>>
>> The worker server tries to keep one application instance open per
>> incoming channel name, but that's no guarantee. It will always make a new
>> instance for different channel names. What kind of behaviour are you
>> expecting?
>>
>> Andrew
>>
>> On Tue, Jun 19, 2018 at 6:45 AM itsnate_b  wrote:
>>
>>> I am using Django 2.0.5, Python 3.6.2, Django Channels 2.1.1, aiohttp
>>> 3.1.3.
>>>
>>> I have created a background worker class which is initialized on a route
>>> to the appropriate event/function. Problem is that it initializes a *new*
>>> instance of the class on every triggered call, meaning that I cannot access
>>> contextual variables (they don't persist). Is there a way to trigger the
>>> same (i.e. first) and only class repeatedly on each routed call? I can
>>> change it all to function calls with global variables stored for the
>>> relevant asyncio futures and/or booleans, but would really prefer not to do
>>> thatany suggestions?
>>>
>>> My (simplified) code:
>>>
>>> *consumers.py*
>>>
>>> from channels.generic.websocket import AsyncWebsocketConsumer
>>> from channels.consumer import AsyncConsumer
>>> from channels.layers import get_channel_layer
>>> import json
>>> import asyncio
>>> import aiohttp
>>> import datetime
>>>
>>>
>>> class mainConsumer(AsyncWebsocketConsumer):
>>>
>>> async def connect(self):
>>> await self.channel_layer.group_add('myGroup', self.channel_name)
>>> await self.accept()
>>>
>>> async def disconnect(self, closeCode):
>>> await self.channel_layer.group_discard('myGroup',
>>> self.channel_name)
>>>
>>> async def receive(self, text_data):
>>> eventData = json.loads(text_data)
>>> print(eventData)
>>> if eventData['toggle'] == 'on':
>>> await self.channel_layer.send('myToggle',{"type":
>>> "openConnection"})
>>> elif eventData['toggle'] == 'off':
>>> await self.channel_layer.send('myToggle',{"type":
>>> "closeConnection"})
>>>
>>> async def myUpdate(self, data):
>>> updateMsg = #some data processing here
>>> await self.send(text_data=json.dumps(updateMsg))
>>>
>>>
>>> class backgroundConsumer(AsyncConsumer):
>>>
>>> def __init__(self, scope):
>>> self.scope = scope
>>> self.turnOnListener = True
>>> self.channelLayer = get_channel_layer()
>>> self.t1 = None
>>> print('inside init') # this will print init on every
>>> channel_layer.send to 'myToggle' in mainConsumer
>>>
>>> async def listen(self):
>>>
>>> async with aiohttp.ClientSession() as session:
>>> async with session.ws_connect('...streaming url...',
>>> ssl=False) as ws:
>>> await ws.send_str('...some string...')
>>> async for msg in ws:
>>> if msg.type == aiohttp.WSMsgType.TEXT:
>>> if msg.data.startswith('2'):
>>> await ws.send_str('3')
>>> else:
>>> await self.parseListenData(msg)
>>>
>>> async def parseListenData(self, msgContent):
>>>  # process and create updateMsg dict with type: 'myUpdate' (so
>>> that it gets to the myUpdate function in main

Re: Django Channels 2.0 - How to init and persist/communicate with only one instance of background worker for every channel_layer.send?

2018-06-21 Thread Andrew Godwin
The worker server tries to keep one application instance open per incoming
channel name, but that's no guarantee. It will always make a new instance
for different channel names. What kind of behaviour are you expecting?

Andrew

On Tue, Jun 19, 2018 at 6:45 AM itsnate_b  wrote:

> I am using Django 2.0.5, Python 3.6.2, Django Channels 2.1.1, aiohttp
> 3.1.3.
>
> I have created a background worker class which is initialized on a route
> to the appropriate event/function. Problem is that it initializes a *new*
> instance of the class on every triggered call, meaning that I cannot access
> contextual variables (they don't persist). Is there a way to trigger the
> same (i.e. first) and only class repeatedly on each routed call? I can
> change it all to function calls with global variables stored for the
> relevant asyncio futures and/or booleans, but would really prefer not to do
> thatany suggestions?
>
> My (simplified) code:
>
> *consumers.py*
>
> from channels.generic.websocket import AsyncWebsocketConsumer
> from channels.consumer import AsyncConsumer
> from channels.layers import get_channel_layer
> import json
> import asyncio
> import aiohttp
> import datetime
>
>
> class mainConsumer(AsyncWebsocketConsumer):
>
> async def connect(self):
> await self.channel_layer.group_add('myGroup', self.channel_name)
> await self.accept()
>
> async def disconnect(self, closeCode):
> await self.channel_layer.group_discard('myGroup',
> self.channel_name)
>
> async def receive(self, text_data):
> eventData = json.loads(text_data)
> print(eventData)
> if eventData['toggle'] == 'on':
> await self.channel_layer.send('myToggle',{"type":
> "openConnection"})
> elif eventData['toggle'] == 'off':
> await self.channel_layer.send('myToggle',{"type":
> "closeConnection"})
>
> async def myUpdate(self, data):
> updateMsg = #some data processing here
> await self.send(text_data=json.dumps(updateMsg))
>
>
> class backgroundConsumer(AsyncConsumer):
>
> def __init__(self, scope):
> self.scope = scope
> self.turnOnListener = True
> self.channelLayer = get_channel_layer()
> self.t1 = None
> print('inside init') # this will print init on every
> channel_layer.send to 'myToggle' in mainConsumer
>
> async def listen(self):
>
> async with aiohttp.ClientSession() as session:
> async with session.ws_connect('...streaming url...',
> ssl=False) as ws:
> await ws.send_str('...some string...')
> async for msg in ws:
> if msg.type == aiohttp.WSMsgType.TEXT:
> if msg.data.startswith('2'):
> await ws.send_str('3')
> else:
> await self.parseListenData(msg)
>
> async def parseListenData(self, msgContent):
>  # process and create updateMsg dict with type: 'myUpdate' (so
> that it gets to the myUpdate function in main Consumer)
>  await self.channelLayer.group_send('myGroup', updateMsg)
>
> async def openConnection(self, event):
> if(self.turnOnListener): *# THIS IS ALWAYS TRUE BECAUSE THE ROUTE
> INITIALIZES A NEW INSTANCE OF THIS CLASS!!*
> await self.channelLayer.group_add(
> 'myGroup',
> self.channel_name,
> )
> self.turnOnListener = False # This only changes it for the
> current instance (obviously)
> loop = asyncio.get_event_loop()
> self.t1 = asyncio.run_coroutine_threadsafe(self.listen(),
> loop=loop)
> else:
> print('already turned on listener')
>
> async def closeConnection(self, event):
> loop = asyncio.get_event_loop()
> loop.call_soon_threadsafe(self.t1.cancel) *# t1 is always None
> because the route initialized a new instance of this class!!*
>
> *routing.py*
> from channels.auth import AuthMiddlewareStack
> from channels.routing import ProtocolTypeRouter, URLRouter,
> ChannelNameRouter
> import dashboard.routing
> from dashboard.consumers import backgroundConsumer
>
> application = ProtocolTypeRouter({
> # (http->django views is added by default)
> 'websocket': AuthMiddlewareStack(
> URLRouter(
> dashboard.routing.websocket_urlpatterns
> )
> ),
> 'channel': ChannelNameRouter({
> "myToggle": backgroundConsumer,
> }),
> })
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> 

Re: Django Channels 2.0 websocket crashing & blocking while using aiohttp

2018-06-17 Thread Andrew Godwin
I'm not quite sure why you seem to be using an AsyncConsumer outside of the
context of the Channels routing framework?

Anyway, it sounds like from what you're describing that your listen()
method is blocking, which makes sense, as it connects to an external
websocket and listens on it for messages forever. Not sure why the crashing
Channels websocket takes down the client one, but maybe they are both made
to crash by some third, external, force?

Andrew

On Sun, Jun 17, 2018 at 11:33 AM Nathan Bluvol 
wrote:

> I am using Django 2.0.5, Python 3.6.2, Django Channels 2.1.1, aiohttp
> 3.1.3.
>
> Background:
> I am connecting to an external websocket, asynchronously, using aiohttp
> within a background channels worker. Once triggered, the socket connection
> stays alive, listening/streaming, and sends incoming messages to a consumer
> through a group_send. The consumer receives it at the appropriate handler,
> however, after some time, it appears that the channels websocket dies and
> the handler can no longer be found - I get 'No handler for message
> type' even though it has been working successfully until that point.
> Second issue is that 'await t1' seems to block in the background. I route
> the 'toggle' to backgroundUpdater.py.
>
> *consumers.py*:
>
> from channels.generic.websocket import AsyncWebsocketConsumer
> import json
>
>
> class MyConsumer(AsyncWebsocketConsumer):
>
> async def connect(self):
> await self.channel_layer.group_add('myGroup', self.channel_name)
> await self.accept()
>
> async def disconnect(self, closeCode):
> await self.channel_layer.group_discard('myGroup',
> self.channel_name)
>
> async def receive(self, text_data):
> eventData = json.loads(text_data)
> print(eventData)
> if eventData['myToggle'] == 'on':
> await self.channel_layer.send('toggle',{"type":
> "openConnection"})
> elif eventData['myToggle'] == 'off':
> await self.channel_layer.send('toggle',{"type":
> "closeConnection"})
>
> async def backgroundWorkerUpdate(self, data):
> await self.send(text_data=json.dumps(data['updateTime']))
>
> *backgroundUpdater.py*:
>
> from channels.consumer import AsyncConsumer
> from channels.layers import get_channel_layer
> import asyncio
> import aiohttp
> import json
> from dashboard.models import Info
> import datetime
>
>
> class backgroundConsumer(AsyncConsumer):
> turnOnListener = True
> channelLayer = get_channel_layer()
> loadComplete = False
>
> async def listen(self):
> infoList = Info.objects.filter(active=True)
> subInfoList = []
> for i in infoList:
> subInfoList.append('info'+str(t))\
> self.loadComplete = False
> async with aiohttp.ClientSession() as session:
> async with session.ws_connect('...streamingsiteURL.../
> socket.io/?EIO=3=websocket', ssl=False) as ws:
> await ws.send_str(str(subInfoList))
> async for msg in ws:
> if msg.type == aiohttp.WSMsgType.TEXT:
> if msg.data.startswith('2'):
> await ws.send_str('3')
> else:
> await self.parseListenData(msg)
>
> async def parseListenData(self, msgContent):
> # handles data in here, including a call to datetime
>
> updateMsg = {'type': 'backgroundWorkerUpdate',
>  'updateTime': str(updateTime),
>  }
> print('built update msg and now sending to group')
> await self.channelLayer.group_send('myGroup', updateMsg)
>
> async def openConnection(self, event):
> if(self.turnOnListener):
> await self.channelLayer.group_add(
> 'myGroup',
> self.channel_name,
> )
> self.turnOnListener = False
> loop = asyncio.get_event_loop()
> t1 = loop.create_task(self.listen())
> await t1
> print('awaiting t1') # THIS NEVER PRINTS UNTIL THE WEBSOCKET
> IN consumers.py CRASHES
> else:
> print('already turned on listener')
>
> async def closeConnection(self, event):
> print('in close connection') # CANNOT SEEM TO TRIGGER THIS ONCE
> await t1 is called above...
> if(not self.turnOnListener):
> print('turn it off')
>
> *Questions*:
> 1. Why is does this work for some time until the websocket in consumers.py
> crashes? I am assuming it has crashed because of the missing handler (i.e.
> 'No handler...' error).
> 2. Why is the 'await t1' call blocking? When I try to send 'await
> self.channel_layer.send('toggleMarket',{"type": "closeConnection"})', it
> sends, but nothing seems to be received?
>
> I am have been trying to get this to run in different ways, however, I
> cannot seem to resolve these two issues. Any help/suggestions would be
> 

Re: Chatbot using Django Channels. Persist a python object in a scope?

2018-06-10 Thread Andrew Godwin
Hi!

You can indeed have things persist for the entire scope - the Consumer (or
any other ASGI app) does exactly this, and so if you're in a consumer you
can just store things on "self". That'll be tied to a specific socket and
garbage-collected once it's done (on Channels 2 only - channels 1 didn't do
this!)

Of course, I am assuming you're meaning "per websocket". If you want to use
another protocol - like the ASGI Telegram server I cooked up - those scopes
are per-user but sometimes get garbage collected after an idle period with
no communication. You should think about this even for WebSocket; what
happens if the user's socket reconnects? Does the state machine reset? If
not, you need to store state somewhere externally from the socket handling
code.

Andrew

Andrew

On Sat, Jun 9, 2018 at 5:05 AM Rithwik Cherian  wrote:

> Hello all,
>
> Very excited to discover the Channels project. I think it could be exactly
> what I was looking for. I am going through the documentation and seeing how
> it works right now, but I would really appreciate if someone could tell me
> if it will help me do what I want.
>
> I am making a chatbot, which is state dependent. The dialogue delivery and
> state management is handled by a Finite State Machine built using the
> transitions (pytransitions )
> library. So my chatbot is a python object, which transitions from one state
> to another based on the users input. So to run the chatbot (per user per
> session) I need something exactly like the scope provided by channels,
> something that lasts for more than one request.
>
> My question is *if I can have a python object instantiated per scope and
> have it persist for the duration of the scope*, so that for each user I
> can have a different object keeping track of which state the user is in and
> returning responses based on the state?
>
> Any help would be appreciated, as I am very inexperienced on things
> related to web (websockets, WSGI, ASGI etc.).
>
> Thanks.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/142cb856-17f7-4cf1-9f3b-87d80e0ca5f8%40googlegroups.com
> 
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAFwN1uq3emdzMgkqncBumhgday%3DjnLZPqeORhc28YfwZu5Fe%2BA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Redirect Loops and 404 with Django Channels and runserver command

2018-06-09 Thread Andrew Godwin
Hi Justin,

I'm going to reply to you on the ticket you opened at
https://github.com/django/channels/issues/1075 with the same question :)

Andrew

On Fri, Jun 8, 2018 at 7:56 AM Justin Lee  wrote:

> I was just trying to integrate channels into an existing django/DRF
> project and I have been running into an issue where my original urls paths
> are breaking when I do runserver. My root index view leads to 301 infinite
> redirects loop and my other app views results in a 404. The Socket server
> is definitely up as I have tried to do the following in console. It is just
> the http views + routes that are somehow breaking
>
>
>
> var chatSocket = new WebSocket('ws://' + window.location.host + '/');
>
> chatSocket.onmessage = function(e) {
>
> var data = JSON.parse(e.data);
>
> var message = data['message'];
>
> console.log(message)
>
> }
>
> chatSocket.send(JSON.stringify({'message': "Hello World"}));
>
>
> OUTPUT:
>
> VM862:4 Hello World
>
>
>
> The app works when I remove "channels" from installed_apps
>
> It also works if I try to run the asgi app through daphne directly
>
> I just want the app to work in django dev runserver
>
>
> I am using django auth backend with login_required for all my views.
>
> I am fairly new to django and would love some help on this!
>
> my_app/urls.py
>
> urlpatterns = [
> path('users/', include('django.contrib.auth.urls')),
> path('admin/', admin.site.urls),
> path('api/', include('api.urls', namespace='api')),
> url(r'^.*', index, name='index'),
> ]
>
> my_app/routing.py
>
> from channels.auth import AuthMiddlewareStackfrom channels.routing import 
> ProtocolTypeRouter, URLRouterfrom django.urls import path
> from api.consumer import EchoConsumer
>
> application = ProtocolTypeRouter({
> 'websocket': AuthMiddlewareStack(
> URLRouter([
> path('', EchoConsumer)
> ])
> )
> })
>
> Settings
>
> # Application definitionINSTALLED_APPS = [
> 'channels',
> 'django.contrib.admin',
> 'django.contrib.auth',
> 'django.contrib.contenttypes',
> 'django.contrib.sessions',
> 'django.contrib.messages',
> 'django.contrib.staticfiles',
> 'django.contrib.postgres',
> 'django_extensions',
> 'rest_framework',
> 'rfs.apps.RfsConfig',
> 'api.apps.ApiConfig',
> 'import_export',
> ]
> MIDDLEWARE = [
> 'django.middleware.security.SecurityMiddleware',
> 'django.contrib.sessions.middleware.SessionMiddleware',
> 'django.middleware.common.CommonMiddleware',
> 'django.middleware.csrf.CsrfViewMiddleware',
> 'django.contrib.auth.middleware.AuthenticationMiddleware',
> 'django.contrib.messages.middleware.MessageMiddleware',
> 'django.middleware.clickjacking.XFrameOptionsMiddleware',
> ]
>
> Versions
>
> channels==2.1.1
> daphne==2.1.2
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/de5f781b-ecca-4c7c-a1f5-3b01fff26531%40googlegroups.com
> 
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAFwN1ur96tzjwFaw%3D_E-NaQoAPH0%2BKamc2QzL2-%2BG50A2hNOXQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: How to test a WebsocketConsumer that has async_to_sync code in it

2018-06-06 Thread Andrew Godwin
Hi - I will reply on the ticket, as I'll be requesting you paste a large
chunk of code and the formatting is better over there!

Andrew

On Wed, Jun 6, 2018 at 12:26 PM 'Artemis' via Django users <
django-users@googlegroups.com> wrote:

> I cannot test my *WebsocketConsumer* ( which includes many *async_to_sync*)
> references using the *WebsocketCommunicator*.
>
> My problem is identical to this closed issue:
> https://github.com/django/channels/issues/944
>
> Can someone give me some hints as to how to proceed without removing the 
> *async_to_sync
> *references in my consumer?
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/8c49a53d-1540-4889-95da-7244b46dcc9c%40googlegroups.com
> 
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAFwN1urTi7T-NEcVGiHSL%3DGokyZx8qeMiSWBciQLrzh2cgRkxw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Django channels with python background tasks

2018-05-26 Thread Andrew Godwin
Hi Jaiswal,

I'm afraid that I can't give detailed help about what your best options are
or walk you through how to do it - that's something you'll have to research
and decide on yourself. Channels allows you to do low-latency
communictation between Django back-ends and JavaScript, but anything you
can do with it can also be done slower using a polling API connection.

My recommendation would be to start simple - doing it using an API that you
poll every few seconds - and then once you have that figured out, look at
how you could improve it using WebSockets and channels.

Andrew

On Sat, May 26, 2018 at 8:38 AM Sourabh Jaiswal 
wrote:

> Hi,
>
>
> I am writing a python based application(CLI Back End) which does telnet to
> some network components and gets some data. It saves the data in sqlite db.
>
>
> For this application I am writing Django based frond end. Which will start
> the CLI app and monitor it. For communication between the CLI App and
> django I am not able to decide what to use. I read somewhere on net that
> django channels can be used in this problem.
>
>
> I have no idea about what django channel is and what it does. Can some one
> please help me in this I need to come up with a tool for this CLI and
> django communication ASAP.
>
>
>
> Thanks in advance!
>
> Regards, Jaiswal.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/2d704873-07cf-41d7-a747-0beb9ac85cfa%40googlegroups.com
> 
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAFwN1uqYWtYwnkPvukJGn9beMfpXSOSWAVEYJQpU%2B50WB%2BvY-w%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Django Channels client not consuming

2018-05-15 Thread Andrew Godwin
Hi,

I'm afraid you're using incredibly old versions of Channels and the bugs
you're encountering are almost certainly fixed in more recent releases. I'm
not sure there's much I can do.

Andrew

On Tue, 15 May 2018, 13:01 LEEPS Lab,  wrote:

> We have an issue implementing django channels in an oTree project (
> http://otree.readthedocs.io/en/latest/)
>
> Currently we are forced to use django channels version 0.17.3 due to the
> restriction inside of oTree, so it is possible that we are dealing with
> version issues.
>
> However, we have an issue implementing a message.reply_channel response
> where we are successfully adding messages to the reply channel queue, but
> our client is not consuming these messages to the point of a
>
> asgiref.inmemory.ChannelLayer.ChannelFull:
>
> Our client code creates the usual websocket consuming scheme
>
> window.onload = function () {
> var ws_scheme = window.location.protocol == "https:" ? "wss" :
> "ws";
> var socket = new WebSocket(ws_scheme + '://' +
> window.location.host + "/hft/{{group.id}}/{{player.id}}/");
> console.log(ws_scheme + '://' + window.location.host + "/hft/{{
> group.id}}/{{player.id}}/");
>
> // Handle any errors that occur.
> socket.onerror = function (error) {
> console.log('WebSocket Error: ' + error);
> };
>
> // Show a connected message when the WebSocket is opened.
> socket.onopen = function (event) {
> console.log('connected to oTree');
> };
> // Handle messages sent by the server.
> socket.onmessage = function (event) {
> // var obj = jQuery.parseJSON(event.data);
> // role = obj.state;
> console.log("Received a message");
> };
> // Show a disconnected message when the WebSocket is closed.
> socket.onclose = function (event) {
> console.log('disconnected from oTree');
> };
>
>
> currently the socket.onmessage function is never called.
>
> Our consumer code looks like this:
>
> class SubjectConsumer(JsonWebsocketConsumer):
>
> def raw_connect(self, message, group_id, player_id):
> Group(group_id).add(message.reply_channel)
> log.info('Player %s is connected to Group %s.' % (player_id,
> group_id))
> self.connect(message, player_id)
>
> def connect(self, message, player_id):
> player = Player.objects.get(id=player_id)
> player.channel = message.reply_channel
> player.save()
>
> def raw_receive(self, message, group_id, player_id):
> msg = json.loads(message.content['text'])
> player = Player.objects.get(id=player_id)
> player.receive_from_client(msg)
>
> def raw_disconnect(self, message, group_id, player_id):
> log = 'Player %s  disconnected from Group %s.' % (player_id,
> group_id)
> logging.info(log)
> Group(group_id).discard(message.reply_channel)
>
> def send(self, msg, chnl):
> Channel(chnl).send(msg)
>
> def broadcast(self, msg, chnl):
> Group(chnl).send(msg)
>
> Usually we will save the reply_channel after connecting with the consumer
> to a player object that will send a message when finished processing
> received data from the client. We have already checked that the channels
> names are correct, and that no errors are thrown in relation to the type of
> our data and how we're sending it.
>
> We believe our routing is correct since we can receive data from the
> client, but here is what we have
>
> channel_routing += [
> route_class(SubjectConsumer,
> path=r"^/hft/(?P\w+)/(?P\w+)/"),
> route_class(InvestorConsumer,
> path=r"^/hft_investor/(?P\w+)/"),
> route_class(JumpConsumer, path=r"^/hft_jump/(?P\w+)/")
> ]
>
> We get the error after we successfully send several messages from the
> consumer's send function but do not receive any on our client websockets.
> We assume that they are sending correctly because we continue execution
> after SubjectConsumer.send() and the queue eventually fills.
>
> 2018-05-15 10:43:06,813 - ERROR - worker - Error processing message with
> consumer oTree_HFT_CDA.consumers.SubjectConsumer:
> Traceback (most recent call last):
>   File "/usr/local/lib/python3.5/dist-packages/channels/worker.py", line
> 120, in run
> consumer(message, **kwargs)
>   File "/usr/local/lib/python3.5/dist-packages/channels/generic/base.py",
> line 31, in __init__
> self.dispatch(message, **kwargs)
>   File "/usr/local/lib/python3.5/dist-packages/channels/generic/base.py",
> line 69, in dispatch
> return self.get_handler(message, **kwargs)(message, **kwargs)
>   File "/home/leeps/oTree_HFT_CDA/oTree_HFT_CDA/consumers.py", line 29, in
> raw_receive
> Channel(str(message.reply_channel)).send({'a':'b'})
>   File "/usr/local/lib/python3.5/dist-packages/channels/channel.py", line
> 

Re: Django-channels request depending Middleware

2018-05-04 Thread Andrew Godwin
That's correct - Django middleware only runs on HTTP request objects, and
so doesn't work in a WebSocket context. The recommended alternative is to
write ASGI middleware for this purpose.

Unfortunately, there is not good documentation on this yet, but you can see
a bit about it here, in the authentication docs:
http://channels.readthedocs.io/en/latest/topics/authentication.html#custom-authentication

Andrew

On Fri, May 4, 2018 at 6:12 AM Marius Räsener  wrote:

> Hey everybody,
>
> We are using a Middleware to inject postgresql schemas into queries based
> on a subdomain. You can have a look how it‘s done here:
>
> https://github.com/systori/systori/blob/dev/systori/apps/company/middleware.py
>
> Now, for channels I‘ve noticed that there is no usual request, right?
> So, if I‘m connecting a WS it doesn‘t hit the Middleware.
> Are there any guides or docs available or any recommendations how to solve
> this properly for channels?
>
> Thx,
> Marius
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/6534824B-4E3B-4E39-8DA0-DF807A84CADC%40mehr-handwerk.de
> 
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAFwN1upH03QzfeycVhHwJ2Bqks96y26npDNZa%2B5etkUWPn0mPw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Channels 2: Consumer lifecycle when run in a worker process

2018-05-02 Thread Andrew Godwin
At the level of abstraction of the Worker, you can't prevent it pulling
more messages off the queue - if you want that level of control, you would
have to subclass it and change the logic yourself, I imagine.

Andrew

On Wed, May 2, 2018 at 1:12 PM Alexander Prokhorov <prok...@gmail.com>
wrote:

>
> Indeed, that is exactly what I am doing - run processing in the background
> with
>
> task = asyncio.ensure_future(database_sync_to_async(self._process)(message
> ))
>
> and then keep track of that running tasks. Actually, I am quite happy with
> this except one thing. I would like to limit the number if messages
> processed at the same time by a single worker. The logic is straightforward
> - once I start a new task I check if the "tasks per worker limit" has
> reached and if so - just invoke
>
> asyncio.wait(self._tasks, return_when=asyncio.FIRST_COMPLETED)
>
> so I block the consumer's message handler until some task finishes. I did
> not make experiments, but from the worker's code
> <https://github.com/django/channels/blob/4500a4252c8459eebe8922533a1a3dd04f1c6e9d/channels/worker.py#L32>
> I can conclude that worker will anyway extract messages from the channel
> and put them into the application's queue which (according to this this
> <https://github.com/django/asgiref/blob/2e92fe245620332a6e57d2e3d1342839758e64b8/asgiref/server.py#L86>)
>  has
> unlimited size. And that is what bothers me the most. My logic is simple,
> if a worker has already reached the "tasks per worker" limit, then I do not
> want this worker to extract message from the channel, cause there is
> probably another worker process willing to process it. Frankly, I do not
> understand how to achieve this... sorry for bothering, but probably you
> have some bright idea, please?
>
> Anyway, thank you for explanation you have already gave me, it helps, and
> it is always pleasure to chat with you :-)
>
> среда, 2 мая 2018 г., 22:35:08 UTC+3 пользователь Andrew Godwin написал:
>>
>> Ah, my apologies - you are entirely right, the scope is the same so it
>> will re-use a single existing instance, which means that it will process
>> messages synchronously and lose them, as you suggested.
>>
>> Using sync_to_async won't help as, while it runs in a threadpool, it also
>> blocks the coroutine until that thread completes.
>>
>> Without modifying the underlying worker implementation, the best way to
>> process things in parallel would be to spin off things into their own
>> coroutines within your handler - either manually, using
>> EventLoop.create_task, or I guess you could slew it out into
>> different-named channels.
>>
>> Andrew
>>
>> On Wed, May 2, 2018 at 11:51 AM Alexander Prokhorov <pro...@gmail.com>
>> wrote:
>>
>>> Andrew, thank you for quick response!
>>>
>>> Unfortunately I see something different. If I raise `StopConsumer`
>>> after processing a single message, the next one is lost. From the code
>>> https://github.com/django/asgiref/blob/master/asgiref/server.py#L75 I
>>> see that `get_or_create_application_instance` returns existing
>>> application instance, so when message arrives it is put into the queue of
>>> the existing application. If I raise `StopConsumer` this application
>>> got killed and the message is lost.
>>>
>>> Another observation: when message handler awaits something (like in my
>>> snippet `await some_process()`) this application instance does not
>>> process new messages. When one message handler function exits - the next
>>> one starts.
>>>
>>> Actually, I was offloading message processing to the threadpool (using `
>>> sync_to_async`) and trying to limit the number of messages being
>>> processed at the same time. I hoped that if I await for some coroutine the
>>> worker will stop accepting messages, so other workers can process them
>>> until this one gets free again. Can you give me a clue how to achieve this
>>> bevaviour?
>>>
>>>
>>> среда, 2 мая 2018 г., 18:39:01 UTC+3 пользователь Andrew Godwin написал:
>>>>
>>>>
>>>>- Will `MyConsumer` receive new `wakeup` messages while awaiting `
>>>>some_process`?
>>>>
>>>> Yes. The worker server runs as many application instances as there are
>>>> messages coming in, even though they all have the same scope. You can see
>>>> the main listening loop here:
>>>> https://github.com/django/channels/blob/master/channels/worker.py#L32
>>>>
>>>>- When do I need to raise 

Re: Channels 2: Consumer lifecycle when run in a worker process

2018-05-02 Thread Andrew Godwin
Ah, my apologies - you are entirely right, the scope is the same so it will
re-use a single existing instance, which means that it will process
messages synchronously and lose them, as you suggested.

Using sync_to_async won't help as, while it runs in a threadpool, it also
blocks the coroutine until that thread completes.

Without modifying the underlying worker implementation, the best way to
process things in parallel would be to spin off things into their own
coroutines within your handler - either manually, using
EventLoop.create_task, or I guess you could slew it out into
different-named channels.

Andrew

On Wed, May 2, 2018 at 11:51 AM Alexander Prokhorov <prok...@gmail.com>
wrote:

> Andrew, thank you for quick response!
>
> Unfortunately I see something different. If I raise `StopConsumer` after
> processing a single message, the next one is lost. From the code
> https://github.com/django/asgiref/blob/master/asgiref/server.py#L75 I see
> that `get_or_create_application_instance` returns existing application
> instance, so when message arrives it is put into the queue of the existing
> application. If I raise `StopConsumer` this application got killed and
> the message is lost.
>
> Another observation: when message handler awaits something (like in my
> snippet `await some_process()`) this application instance does not
> process new messages. When one message handler function exits - the next
> one starts.
>
> Actually, I was offloading message processing to the threadpool (using `
> sync_to_async`) and trying to limit the number of messages being
> processed at the same time. I hoped that if I await for some coroutine the
> worker will stop accepting messages, so other workers can process them
> until this one gets free again. Can you give me a clue how to achieve this
> bevaviour?
>
>
> среда, 2 мая 2018 г., 18:39:01 UTC+3 пользователь Andrew Godwin написал:
>>
>>
>>- Will `MyConsumer` receive new `wakeup` messages while awaiting `
>>some_process`?
>>
>> Yes. The worker server runs as many application instances as there are
>> messages coming in, even though they all have the same scope. You can see
>> the main listening loop here:
>> https://github.com/django/channels/blob/master/channels/worker.py#L32
>>
>>- When do I need to raise `StopConsumer`? I can do it after each
>>processing of `wakeup` message (like in the code above) is that
>>correct? What will happen with all the `pending` messages in such
>>case?
>>
>> You need to raise it when the specific application instance you have is
>> completed - because there's a different instance for each message, that
>> means "raise when you've finished processing the message". Nothing happens
>> to other messages as they're being handled by different instances.
>>
>> Andrew
>>
>> On Wed, May 2, 2018 at 7:50 AM Alexander Prokhorov <pro...@gmail.com>
>> wrote:
>>
>>> Dear Andrew,
>>>
>>> I would like to ask couple of questions about the lifecycle of consumers
>>> running in Channels workers and serving custom channels.
>>>
>>> Consider a consumer:
>>>
>>> # myconsumer.py
>>> class MyConsumer(channels.consumer.AsyncConsumer):
>>> async def wakeup(self, message):
>>>  await some_process()
>>>  raise channels.exceptions.StopConsumer()
>>>
>>> which I "register" to process messages in the channel `my_channel`:
>>>
>>> # routing.py
>>> application = channels.routing.ProtocolTypeRouter({
>>> 'channel': channels.routing.ChannelNameRouter({
>>> 'my_channel': MyConsumer
>>> })
>>> })
>>>
>>> and eventually I run designated Channels worker to process `my_channel`
>>> messages.
>>>
>>> ./manage.py runworker my_channel
>>>
>>> So the questions are:
>>>
>>>- Will `MyConsumer` receive new `wakeup` messages while awaiting `
>>>some_process`?
>>>- When do I need to raise `StopConsumer`? I can do it after each
>>>processing of `wakeup` message (like in the code above) is that
>>>correct? What will happen with all the `pending` messages in such
>>>case?
>>>
>>> Actually, I do not raise "StopConsumer" in the implementation I
>>> currently have, but this leads to an issue with tests. In tests I need to
>>> somehow wait until all workers finish processing their messages. I tried
>>> calling `channels.testing.ApplicationCommunicator.wait()` but as I see it
>>> from the

Re: Channels 2: Consumer lifecycle when run in a worker process

2018-05-02 Thread Andrew Godwin
   - Will `MyConsumer` receive new `wakeup` messages while awaiting `
   some_process`?

Yes. The worker server runs as many application instances as there are
messages coming in, even though they all have the same scope. You can see
the main listening loop here:
https://github.com/django/channels/blob/master/channels/worker.py#L32

   - When do I need to raise `StopConsumer`? I can do it after each
   processing of `wakeup` message (like in the code above) is that correct?
   What will happen with all the `pending` messages in such case?

You need to raise it when the specific application instance you have is
completed - because there's a different instance for each message, that
means "raise when you've finished processing the message". Nothing happens
to other messages as they're being handled by different instances.

Andrew

On Wed, May 2, 2018 at 7:50 AM Alexander Prokhorov 
wrote:

> Dear Andrew,
>
> I would like to ask couple of questions about the lifecycle of consumers
> running in Channels workers and serving custom channels.
>
> Consider a consumer:
>
> # myconsumer.py
> class MyConsumer(channels.consumer.AsyncConsumer):
> async def wakeup(self, message):
>  await some_process()
>  raise channels.exceptions.StopConsumer()
>
> which I "register" to process messages in the channel `my_channel`:
>
> # routing.py
> application = channels.routing.ProtocolTypeRouter({
> 'channel': channels.routing.ChannelNameRouter({
> 'my_channel': MyConsumer
> })
> })
>
> and eventually I run designated Channels worker to process `my_channel`
> messages.
>
> ./manage.py runworker my_channel
>
> So the questions are:
>
>- Will `MyConsumer` receive new `wakeup` messages while awaiting `
>some_process`?
>- When do I need to raise `StopConsumer`? I can do it after each
>processing of `wakeup` message (like in the code above) is that
>correct? What will happen with all the `pending` messages in such case?
>
> Actually, I do not raise "StopConsumer" in the implementation I currently
> have, but this leads to an issue with tests. In tests I need to somehow
> wait until all workers finish processing their messages. I tried calling
> `channels.testing.ApplicationCommunicator.wait()` but as I see it from the
> code it waits the application/consumer to finish, i.e. to raise
> `StopConsumer` exception. Probably you can share some recommendations.
> Thanks in advance.
>
> Best regards,
> Alexander.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/00e6398b-a71f-4509-a95b-3ced88b26ee0%40googlegroups.com
> 
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAFwN1urq6%2B7GJO-8rA-0V%3DmzmCe5_1OvF-ix8H7phOFB5H8BCw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Architecting a Crypto Market Data Feed using Django Channels

2018-04-28 Thread Andrew Godwin
I can't help you with real-time streaming architecture overall - that's a
much bigger scope of thing - but I can say that you shouldn't be keeping a
synchronous consumer open like that (you're using a whole thread). You
should either rewrite it to be async-native, so it doesn't use up a thread
and potentially block the server, or rework it to put the feed events onto
the channel layer from an external process.

Andrew

On Sat, Apr 28, 2018 at 1:12 AM, Michael 
wrote:

> Hi,
>
> What is the best way to architect a Django Channels app that provides a
> very fast infinite stream of market data? This is what I have so far, but I
> think it's not the best solution.
>
> This data is updated every millisecond so I would prefer to not persist it
> (unless there is a way of using redis pub/sub without actually saving the
> data, only for messaging)
>
>
>
>
> class ChatConsumer(WebsocketConsumer):
> def connect(self):
> self.room_name = 'foo'
> self.room_group_name = 'foo'
> async_to_sync(self.channel_layer.group_add)(
> self.room_group_name,
> self.channel_name
> )
>
>
> self.accept()
> while True:
>   # Imagine this is another WS feed or Zero MQ Feed.
>   feed = Feed()
>   for event in feed:
>   if event.name == "text":
>   data = event.json
>   self.send(str(data)
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit https://groups.google.com/d/
> msgid/django-users/01bf458c-ff1a-4cf6-bd58-da9b2f43123c%40googlegroups.com
> 
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAFwN1uqD0_qaNksKFo-OZAG3Lmbg023fWr2eEhct%2BGW-JWwoCw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Django Channels also works when I have no Worker running.

2018-04-17 Thread Andrew Godwin
Channels 2 does not run requests in workers. Workers are now only for
running background tasks on specific channels, and so you do not need them
normally.

Andrew

On Tue, Apr 17, 2018 at 6:54 AM, Christopher Wittlinger <
cwittlinge...@gmail.com> wrote:

> I think I misunderstood the concept of the workers. I thought that all
> Requests (HTTP and Websocket) are delegated to some sort of consumers which
> is run in a Worker.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit https://groups.google.com/d/
> msgid/django-users/aed6a302-2e41-4aa6-bedf-86b842b9181b%40googlegroups.com
> 
> .
>
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAFwN1urhtNdxhT3kPht-0%3DK6%2BTN4rz0GAbKfUbtKC0mTqEdWnw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: wrong imports channel ?

2018-04-12 Thread Andrew Godwin
Channels 2 has different objects and import paths - code from Channels 1
(like you have there) isn't compatible.

Andrew

On Thu, Apr 12, 2018 at 12:37 AM, Jules LE BRIS 
wrote:

> Hi,
>
> I would like to do something like this in views.py :
>
> message = {"message":msg}
> Channel('chat').send(message, immediately=True)
>
> but the import : from channels import Channel doen't work. I'm using
> channels==2.0.2.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit https://groups.google.com/d/
> msgid/django-users/b7684c27-9568-4321-a70b-29e58c550b3d%40googlegroups.com
> 
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAFwN1uqCuYwJYkFpnQ%3DtcVXjNNZQi-5fx9GNj%2BmZkADntv1jLQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Using channels, how to send a notification to single user by model changed trigger event?

2018-04-07 Thread Andrew Godwin
Make a Group per user, and then send to that group. You should treat a user
as a group as they may have multiple windows or tabs open.

Andrew

On Sat, Apr 7, 2018 at 7:02 AM,  wrote:

> This example  showed
> in django project how to use channels package (channel 2).
> But here I can't find out sending data to a single user.
> I'm trying to send notification to user by using channels package.
> If someone know, pls tell me.
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit https://groups.google.com/d/
> msgid/django-users/4c8a09ba-9632-411c-bc95-fd805c3d0ece%40googlegroups.com
> 
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAFwN1urv%3D_5HkNH5BCzop1CqnMTDGqY_2hB86jFNkNe9ZyryaA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Mysql is gone away auth middleware channels 2

2018-04-05 Thread Andrew Godwin
You must do all database access through a synchronous function wrapped in
database_sync_to_async - you can't just call it directly on an asyncio
thread:
http://channels.readthedocs.io/en/latest/topics/databases.html#database-sync-to-async

Andrew

On Thu, Apr 5, 2018 at 5:25 AM, Matteo Lucchesi  wrote:

> https://codeshare.io/5wlXNK
>
> Il giorno giovedì 5 aprile 2018 14:07:56 UTC+2, Matteo Lucchesi ha scritto:
>>
>> i've write a auth middleware for django channels :
>>
>> https://codeshare.io/aYnQmN
>>
>> but seem that use a persistent connection and after some hour i have the
>> erro "Mysql is gone away"
>>
>> Where is the error?
>>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit https://groups.google.com/d/
> msgid/django-users/c8171fd2-a423-439e-b89c-813a747658a1%40googlegroups.com
> 
> .
>
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAFwN1ur-EMmQiwf34yovNYck5SzO2B5_jYBv9cKv8F4daNbGHg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Channels 2.0 max number of connections

2018-04-05 Thread Andrew Godwin
No, there is no hard limit programmed into Daphne. It will keep accepting
as long as it can.

Andrew

On Wed, Apr 4, 2018 at 11:30 PM, Алексей Кузуб <ronixx...@gmail.com> wrote:

> But no limit in params or in some other place?
>
> среда, 4 апреля 2018 г., 20:00:11 UTC+3 пользователь Andrew Godwin написал:
>>
>> It depends on your server and how busy the connections are. If you need
>> more you should run more instances of Daphne.
>>
>> Andrew
>>
>> On Wed, 4 Apr 2018, 05:21 Алексей Кузуб, <roni...@gmail.com> wrote:
>>
>>> Hello! How many connections Channels 2.0 can handle and keep open
>>> simultaneously? And how should I configure Daphne that increases this
>>> number?
>>>
>>> --
>>> You received this message because you are subscribed to the Google
>>> Groups "Django users" group.
>>> To unsubscribe from this group and stop receiving emails from it, send
>>> an email to django-users...@googlegroups.com.
>>> To post to this group, send email to django...@googlegroups.com.
>>> Visit this group at https://groups.google.com/group/django-users.
>>> To view this discussion on the web visit https://groups.google.com/d/ms
>>> gid/django-users/641b59f8-ed76-4a08-a7f2-06963e7d183f%40googlegroups.com
>>> <https://groups.google.com/d/msgid/django-users/641b59f8-ed76-4a08-a7f2-06963e7d183f%40googlegroups.com?utm_medium=email_source=footer>
>>> .
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit https://groups.google.com/d/
> msgid/django-users/9602d8c9-3316-4455-b14c-9da9e20217b8%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/9602d8c9-3316-4455-b14c-9da9e20217b8%40googlegroups.com?utm_medium=email_source=footer>
> .
>
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAFwN1urHDayBr1%2B%2BbmTATNhSPKNiXvDLfiOTfEHZmF%2BJAbFh0g%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Channels 2.0 max number of connections

2018-04-04 Thread Andrew Godwin
It depends on your server and how busy the connections are. If you need
more you should run more instances of Daphne.

Andrew

On Wed, 4 Apr 2018, 05:21 Алексей Кузуб,  wrote:

> Hello! How many connections Channels 2.0 can handle and keep open
> simultaneously? And how should I configure Daphne that increases this
> number?
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/641b59f8-ed76-4a08-a7f2-06963e7d183f%40googlegroups.com
> 
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAFwN1uqjoAhWtc6mdmECNV16qdcB9j4wWrTE4u4k%2BGU1KvxmAA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: [django-channels] Which process sends message to all channels in a group during group_send?

2018-03-30 Thread Andrew Godwin
When you do a group send, the message doing the sending fans out the send
to all the target channels (currently using individual commands, and soon
with a Lua script inside Redis). We're not using redis pub-sub - we use
lists instead.

Andrew

On Fri, Mar 30, 2018 at 2:26 AM, krdeepak  wrote:

> Hi,
>
> I was doing a deep dive in django channels and related projects, and plan
> to use it in my project along with contributing to it in the future.
> I understood most of the details, but still not clear about one things.
>
> During a group_send in channel_layer, which process is responsible for
> listening to this message and then broadcasting that message to all the
> channels in that group?
> Which process is consuming messages in channel layer?
>
> Seems I am missing some details on how redis implements pub-sub.
>
> Thanks,
> Kr Deepak
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit https://groups.google.com/d/
> msgid/django-users/d8018f8e-614e-4456-b534-b8bb17d5bf3f%40googlegroups.com
> 
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAFwN1uqDa0e5SfMtW_4EXKbJ9y82ubA_Ym7dZ5HgvSkMXaw12g%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Getting channels 2 to work

2018-03-30 Thread Andrew Godwin
That code would be for Channels 1. Channels 2 is an entirely different
import set - make sure you are using tutorials or answers specifically for
2.

Andrew

On Thu, Mar 29, 2018 at 4:52 PM, Luke Waltman <lwalt...@gmail.com> wrote:

> Hello Everyone,
>
> I am still having an issue with Channels that was never really solved in
> an earlier Stackoverflow question...
>
> Why does :
>
> from channels.asgi import get_channel_layer
>
> result in :
>
> from channels.asgi import get_channel_layerImportError: No module named asgi
>
> I am using Django (2.03), Channels (2.02) and Python(3.5.1) within a
> virtual environment.  I also installed asgi_redis.
>
> -Luke
>
> On Monday, December 4, 2017 at 11:11:14 AM UTC-6, Andrew Godwin wrote:
>>
>> Hi John,
>>
>> The ProtocolTypeRouter currently does this for you automatically as long
>> as you don't specify a `http` handler. Otherwise, the ASGI application that
>> runs Django's view system is channels.http.AsgiHandler
>>
>> Andrew
>>
>> On Mon, Dec 4, 2017 at 7:56 AM, John Wayne <nrftes...@gmail.com> wrote:
>>
>>> Hi Andrew,
>>>
>>> first of all thanks for your reply. With your answer and the information
>>> from your blog I was able to get my asgi application running.
>>>
>>> Now I want to route all http traffic to django's viewsystem back as i
>>> dont need channel's http handling. On your blog you posted this snippet
>>>
>>> application = ProtocolTypeRouter({
>>> "http": URLRouter([
>>> url("^", DjangoViewSystem),
>>> ]),
>>>
>>> But how should the DjangoViewSystem class look like? There is no such
>>> file included.
>>>
>>>
>>> John
>>>
>>> On Friday, December 1, 2017 at 6:22:06 PM UTC+1, Andrew Godwin wrote:
>>>>
>>>> Hi John,
>>>>
>>>> It's not in an end-user-useable state quite yet, so apologies for it
>>>> being hard to install. Crucially, the docs on ASGI_APPLICATION aren't
>>>> written yet as I'm still working on authentication stuff!
>>>>
>>>> A brief guide is:
>>>>
>>>>  - Make an asgi.py as specified in http://channels.readthedocs
>>>> .io/en/2.0/deploying.html
>>>>  - Make a channels.routing.ProtocolTypeRouter in your project's
>>>> routing.py and configure this as needed (you'll have to read the code for
>>>> now)
>>>>  - Point ASGI_APPLICATION to that root router.
>>>>
>>>> I'll have a lot more docs and stuff coming soon as I start prepping
>>>> Channels 2 for a beta release, which will include routing and setup
>>>> examples.
>>>>
>>>> Andrew
>>>>
>>>> On Fri, Dec 1, 2017 at 6:03 AM, John Wayne <nrftes...@gmail.com> wrote:
>>>>
>>>>> Hi everyone,
>>>>>
>>>>> I am stuck getting the new channels 2 to a working state. I installed
>>>>> django==2.0rc1,  and channels==2.0.x, daphne==2.0.x, asgiref==2.0.x from
>>>>> the git repo
>>>>> channels is enabled inside the settings.py. I also created the asgi.py
>>>>> file in the direcotry of the wsgi.py file acording to the docs. But still 
>>>>> i
>>>>> get this error
>>>>>
>>>>> raise ImproperlyConfigured("Cannot find ASGI_APPLICATION setting.")
>>>>> django.core.exceptions.ImproperlyConfigured: Cannot find
>>>>> ASGI_APPLICATION setting.
>>>>>
>>>>> So it seems that ASGI_APPLICATION has to be specified. So after adding
>>>>> the following line:
>>>>> ASGI_APPLICATION='mytestproject.asgi.application'
>>>>> and starting ./manage.py runserver it now complains again. So which
>>>>> value is supposed to be there?
>>>>>
>>>>>   raise ImproperlyConfigured("Cannot find %r in ASGI_APPLICATION
>>>>> module %s" % (name, path))
>>>>> django.core.exceptions.ImproperlyConfigured: Cannot find
>>>>> 'application' in ASGI_APPLICATION module mytestproject.asgi
>>>>>
>>>>> I need to stick to version 2 as i want to try out asgigram (
>>>>> https://github.com/andrewgodwin/asgigram).
>>>>>
>>>>> Thanks
>>>>>
>>>>> --
>>>>> You received this message because you are subscribed to the Google
>>>&g

Re: Does Daphne of Django Channels send out a heartbeat to keep the connection alive?

2018-03-29 Thread Andrew Godwin
The underlying Daphne code will send PING requests to the client
periodically, adjustable with --ping-interval. If it doesn't get a response
back before --ping-timeout, it will close the connection.

Heroku have their own loadbalancer, so it's possible that is changing how
the sockets behave.

Andrew

On Thu, Mar 29, 2018 at 7:52 AM, lakeshow  wrote:

> Is this even done server side?  Or does it need to be implemented with js
> for the client?
>
> I ask because it appears that my connection repeatedly before or during
> the attempt to connect. It only happens when uploaded to Heroku's servers
> as well.
>
>
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit https://groups.google.com/d/
> msgid/django-users/0829ae5b-c03b-44f1-b8de-9c237153bc59%40googlegroups.com
> 
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAFwN1uoQNjOyBZ%3DiEJ%2BiV6mExPdMLQNKr4KiYdfLifWiwpooLA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Django Channels 2 poor performance and high CPU usage

2018-03-27 Thread Andrew Godwin
Not getting past HANDSHAKING with the in-memory layer is a bit weird, and
scope is just a normal Python dictionary. Is it possible to put your code
up somewhere in a simple form so I can look over it?

Andrew

On Tue, Mar 27, 2018 at 4:11 AM, 'James Foley' via Django users <
django-users@googlegroups.com> wrote:

> It looks like accessing scope for url kwargs is a big hit on performance.
> In fact hitting scope for anything seems to introduce some form of delay.
>
> I'm getting very mixed results so I'm unsure if this is now an issue with
> redis. It is definitely the python process using 100% of the CPU though.
>
>
> On Tuesday, 27 March 2018 09:51:44 UTC+1, James Foley wrote:
>>
>> Apologies for the double post, I've removed the other one.
>>
>> PYTHONASYNCIODEBUG doesn't appear to give me any warnings.
>>
>> This test I am running is only between two users, each user belonging to
>> the same group. All messages received are pushed back out to all users and
>> filtered clientside.
>>
>> Assuming I use 'channels.layers.InMemoryChannelLayer' as my backend for
>> the memory layer, none of my websocket connections get past HANDSHAKING so
>> I'm not exactly sure whats up there.
>>
>> On Monday, 26 March 2018 17:35:34 UTC+1, Andrew Godwin wrote:
>>>
>>> (You double-posted this so I'm just going to reply to this one)
>>>
>>> I need to know a bit more information about what the slowdown is - in
>>> particular:
>>>
>>> * Have you run with PYTHONASYNCIODEBUG=1 set as an environment variable
>>> to check for non-yielding coroutines?
>>> * What sort of messages per second are we talking about? You say every
>>> 20ms, but how many listening on the group (so what does it multiply out to?)
>>> * Do you get the same CPU usage issues if you try using the in-memory
>>> channel layer rather than the Redis one?
>>>
>>> The last point would be especially interesting to check, as the node
>>> relay server you built does not, I imagine, use Redis as a cross-server
>>> transport in the middle and so that would be the major difference. 200
>>> requests a second should still be fine, though, so the others are worth
>>> knowing about as well.
>>>
>>> One further thing you could do is build a simple echo server with no use
>>> of groups or the channel layer at all and see how that performs, to narrow
>>> down where the performance issue lies.
>>>
>>> Andrew
>>>
>>> On Mon, Mar 26, 2018 at 6:15 AM, James <jamesrich...@gmail.com> wrote:
>>>
>>>> I'm using Channels 2 to build a shared 3D model viewing tool, but I'm
>>>> running into performance issues where Channels can't keep up and uses 100%
>>>> of a single core. This results in clients just receiving a slow trickle of
>>>> messages rather than the fast stream I was expecting.
>>>>
>>>> I ended up stripping my consumer all the way back to basically a relay
>>>> server, so a client sends a message, server broadcasts the exact data
>>>> received to a group. I am sending a lot of data though (a message every
>>>> 20ms or so) so not sure if that is causing my issues.
>>>>
>>>> Using node I built the same relay server and I have zero issues with
>>>> speed or server performance.
>>>>
>>>> Is this down to how many workers I have running vs the data I'm
>>>> sending, or perhaps the overhead of Django?
>>>>
>>>> --
>>>> You received this message because you are subscribed to the Google
>>>> Groups "Django users" group.
>>>> To unsubscribe from this group and stop receiving emails from it, send
>>>> an email to django-users...@googlegroups.com.
>>>> To post to this group, send email to django...@googlegroups.com.
>>>> Visit this group at https://groups.google.com/group/django-users.
>>>> To view this discussion on the web visit https://groups.google.com/d/ms
>>>> gid/django-users/fecbabcc-2692-4d7f-ac6f-6de7c3631354%40goog
>>>> legroups.com
>>>> <https://groups.google.com/d/msgid/django-users/fecbabcc-2692-4d7f-ac6f-6de7c3631354%40googlegroups.com?utm_medium=email_source=footer>
>>>> .
>>>> For more options, visit https://groups.google.com/d/optout.
>>>>
>>>
>>> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emai

Re: Django Channels 2 poor performance and high CPU usage

2018-03-26 Thread Andrew Godwin
(You double-posted this so I'm just going to reply to this one)

I need to know a bit more information about what the slowdown is - in
particular:

* Have you run with PYTHONASYNCIODEBUG=1 set as an environment variable to
check for non-yielding coroutines?
* What sort of messages per second are we talking about? You say every
20ms, but how many listening on the group (so what does it multiply out to?)
* Do you get the same CPU usage issues if you try using the in-memory
channel layer rather than the Redis one?

The last point would be especially interesting to check, as the node relay
server you built does not, I imagine, use Redis as a cross-server transport
in the middle and so that would be the major difference. 200 requests a
second should still be fine, though, so the others are worth knowing about
as well.

One further thing you could do is build a simple echo server with no use of
groups or the channel layer at all and see how that performs, to narrow
down where the performance issue lies.

Andrew

On Mon, Mar 26, 2018 at 6:15 AM, James  wrote:

> I'm using Channels 2 to build a shared 3D model viewing tool, but I'm
> running into performance issues where Channels can't keep up and uses 100%
> of a single core. This results in clients just receiving a slow trickle of
> messages rather than the fast stream I was expecting.
>
> I ended up stripping my consumer all the way back to basically a relay
> server, so a client sends a message, server broadcasts the exact data
> received to a group. I am sending a lot of data though (a message every
> 20ms or so) so not sure if that is causing my issues.
>
> Using node I built the same relay server and I have zero issues with speed
> or server performance.
>
> Is this down to how many workers I have running vs the data I'm sending,
> or perhaps the overhead of Django?
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit https://groups.google.com/d/
> msgid/django-users/fecbabcc-2692-4d7f-ac6f-6de7c3631354%40googlegroups.com
> 
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAFwN1upArogs2bWDL3TsqpPQDX%3D439ps98PxVCpwnJtM1%3D5feg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Django channels 2 and data binding

2018-03-25 Thread Andrew Godwin
You can write most of the data binding functionality yourself in Channels 2
by adding group send methods to model save methods, but if Channels 1 is
working for you right now, I wouldn't move until you're forced to.

Andrew

On Sat, Mar 24, 2018 at 11:07 PM, Fabio Andrés García Sánchez <
fabio.garcia.sanc...@gmail.com> wrote:

> Thank Andrew.
> I would like to know if it is a good idea to keep using django 1.x to get
> data binding or would you recomend another path to keep update my frontend
> with my lastest data?
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit https://groups.google.com/d/
> msgid/django-users/8552069b-fd61-48a2-85e9-ff1c2ee5a2e3%40googlegroups.com
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAFwN1uq%2BgfqMfysfhAwwATNcWdna%2BGmh3Hs-cYH2gjx7QDmRtg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Django channels 2 and data binding

2018-03-24 Thread Andrew Godwin
There are no examples yet - I removed data binding from core Channels in
the 2.0 release because it had a high maintenance overhead and as yet
there's not a replacement. I was hoping someone might volunteer to help
build it as an external package but if not, I may eventually get time to
write it later in the year.

Andrew

On Sat, Mar 24, 2018 at 7:07 PM, Fabio Andrés García Sánchez <
fabio.garcia.sanc...@gmail.com> wrote:

> Is there any example about how to use data binding with django channels? I
> could not find any source to insertando how to do it.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit https://groups.google.com/d/
> msgid/django-users/1c3803e2-a049-47d3-ab11-25f58b06a826%40googlegroups.com
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAFwN1uqdx%3DZ4jSxNran%3D%3DQ%2BUPNyHZfUwMnR%3DC9JKziwDgObt4Q%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Overriding channel-layer's group_send and group_add to add persistence

2018-03-23 Thread Andrew Godwin
I would check the connection is going to the right server/database as well?
But past that, I can't help you - I'd try doing some things with plain
aioredis to see if you can replicate it there.

Andrew

On Fri, Mar 23, 2018 at 9:01 AM, 'Alex' via Django users <
django-users@googlegroups.com> wrote:

> Hi,
>
> I've used the redis-cli to get the contents of the key, and it has filled
> it properly, so the information is definitely in redis under that key. The
> issue seems to be that message = await connection.get(pers_key) always
> returns none. One thing I'm certain of is that it's in redis!
>
> Alex
>
> On Friday, 23 March 2018 15:58:37 UTC, Andrew Godwin wrote:
>>
>> It looks correct at first glance - I would insert a debugger there and
>> see what the Redis database contained manually at that point.
>>
>> Andrew
>>
>> On Fri, Mar 23, 2018 at 2:56 AM, 'Alex' via Django users <
>> django...@googlegroups.com> wrote:
>>
>>> I've been trying to add persistence to channel layers, such that each
>>> new consumer joining a group is sent the most recent message from that
>>> group, on connect. Below are my attempts. For some reason, the message in
>>> the highlighted line always seems to be of type 'None'. Am I going about
>>> this completely incorrectly? I'd be really grateful for any help.
>>>
>>>
>>> from channels_redis.core import RedisChannelLayer
>>> from channels.exceptions import ChannelFull
>>> import time
>>>
>>>
>>> class RedisChannelLayerGroupPersistence(RedisChannelLayer):
>>>
>>>
>>>
>>>
>>> async def group_send(self, group, message):
>>> """
>>> Sends a message to the entire group.
>>> """
>>> assert self.valid_group_name(group), "Group name not valid"
>>> # Retrieve list of all channel names
>>> key = self._group_key(group)
>>> pers_key = str(key) + "_PERS"
>>> async with self.connection(self.consistent_hash(group)) as
>>> connection:
>>> # Discard old channels based on group_expiry
>>> await connection.zremrangebyscore(key, min=0, max=int(time.
>>> time()) - self.group_expiry)
>>> # Return current lot
>>> channel_names = [
>>> x.decode("utf8") for x in
>>> await connection.zrange(key, 0, -1)
>>> ]
>>> # TODO: More efficient implementation (lua script per shard?)
>>>  try:
>>> await connection.persist(pers_key)
>>> await connection.set(pers_key, str(message))
>>> print("TYPE = 
>>> {}".format(type(str(message
>>>
>>>
>>> for channel in channel_names:
>>> try:
>>> await self.send(channel, message)
>>> except ChannelFull:
>>> pass
>>>
>>>
>>> async def group_add(self, group, channel):
>>>
>>>
>>> """
>>> Adds the channel name to a group.
>>> """
>>> # Check the inputs
>>> assert self.valid_group_name(group), "Group name not valid"
>>> assert self.valid_channel_name(channel), "Channel name not
>>> valid"
>>> # Get a connection to the right shard
>>> group_key = self._group_key(group)
>>> pers_key = str(group_key) + "_PERS"
>>> async with self.connection(self.consistent_hash(group)) as
>>> connection:
>>> message = await connection.get(pers_key) #ISSUE HERE
>>> -- MESSAGE IS NONE
>>> # Add to group sorted set with creation time as timestamp
>>> await connection.zadd(
>>> group_key,
>>> time.time(),
>>> channel,
>>> )
>>> # Set expiration to be group_expiry, since everything in
>>> # it at this point is guaranteed to expire before that
>>> try:
>>> await self.send(channel, str(message))
>>> except ChannelFull:
>>> pass
>>>
>>>
>>>
>>>
>>> awa

Re: channels and middlewares

2018-03-23 Thread Andrew Godwin
Check your Django version - MIDDLEWARE_CLASSES is deprecated, you should be
using the new MIDDLEWARE setting.

Andrew

On Fri, Mar 23, 2018 at 8:56 AM, Matteo Lucchesi 
wrote:

> It' possible that running a django application with channel2 ingores my
> MIDDLEWARE_CLASSES over HTTP?
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit https://groups.google.com/d/
> msgid/django-users/e68b23ee-5f0c-4370-9730-c56672c28cb3%40googlegroups.com
> 
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAFwN1uoZwTTGwhvxWnzsrcF8mwiGjSFm8sYV4%2BewezVKHYb4mQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Overriding channel-layer's group_send and group_add to add persistence

2018-03-23 Thread Andrew Godwin
It looks correct at first glance - I would insert a debugger there and see
what the Redis database contained manually at that point.

Andrew

On Fri, Mar 23, 2018 at 2:56 AM, 'Alex' via Django users <
django-users@googlegroups.com> wrote:

> I've been trying to add persistence to channel layers, such that each new
> consumer joining a group is sent the most recent message from that group,
> on connect. Below are my attempts. For some reason, the message in the
> highlighted line always seems to be of type 'None'. Am I going about this
> completely incorrectly? I'd be really grateful for any help.
>
>
> from channels_redis.core import RedisChannelLayer
> from channels.exceptions import ChannelFull
> import time
>
>
> class RedisChannelLayerGroupPersistence(RedisChannelLayer):
>
>
>
>
> async def group_send(self, group, message):
> """
> Sends a message to the entire group.
> """
> assert self.valid_group_name(group), "Group name not valid"
> # Retrieve list of all channel names
> key = self._group_key(group)
> pers_key = str(key) + "_PERS"
> async with self.connection(self.consistent_hash(group)) as
> connection:
> # Discard old channels based on group_expiry
> await connection.zremrangebyscore(key, min=0, max=int(time.
> time()) - self.group_expiry)
> # Return current lot
> channel_names = [
> x.decode("utf8") for x in
> await connection.zrange(key, 0, -1)
> ]
> # TODO: More efficient implementation (lua script per shard?)
>  try:
> await connection.persist(pers_key)
> await connection.set(pers_key, str(message))
> print("TYPE = 
> {}".format(type(str(message
>
>
> for channel in channel_names:
> try:
> await self.send(channel, message)
> except ChannelFull:
> pass
>
>
> async def group_add(self, group, channel):
>
>
> """
> Adds the channel name to a group.
> """
> # Check the inputs
> assert self.valid_group_name(group), "Group name not valid"
> assert self.valid_channel_name(channel), "Channel name not valid"
> # Get a connection to the right shard
> group_key = self._group_key(group)
> pers_key = str(group_key) + "_PERS"
> async with self.connection(self.consistent_hash(group)) as
> connection:
> message = await connection.get(pers_key) #ISSUE HERE
> -- MESSAGE IS NONE
> # Add to group sorted set with creation time as timestamp
> await connection.zadd(
> group_key,
> time.time(),
> channel,
> )
> # Set expiration to be group_expiry, since everything in
> # it at this point is guaranteed to expire before that
> try:
> await self.send(channel, str(message))
> except ChannelFull:
> pass
>
>
>
>
> await connection.expire(group_key, self.group_expiry)
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit https://groups.google.com/d/
> msgid/django-users/abc8747d-8d80-4ec4-a2ca-d5e91c161c08%40googlegroups.com
> 
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAFwN1up6Mq3SHnvLb6G2hvhf_cW%3DDzCZjapBLNQuBMDhJMmAWA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Django 2.0 and AsgiRequest Error

2018-03-20 Thread Andrew Godwin
Hi - did you also open this issue on GitHub:
https://github.com/django/channels/issues/981 ?

In either case, your MIDDLEWARE setting would be useful, this looks like a
middleware is not running.

Andrew

On Tue, Mar 20, 2018 at 2:47 PM, G Broten  wrote:

> Django Users:
>  I've done a Google search on this error, but I haven't found anything
> relevant:
>  AttributeError: 'AsgiRequest' object has no attribute 'user'
>
> So I'm hoping someone has an insight into its root cause and, maybe, a
> solution.
> The full error is shown below:
>
> 2018-03-20 15:40:31,079 ERRORInternal Server Error: /message/
> Traceback (most recent call last):
>   File "/usr/lib/python3.6/site-packages/django/core/handlers/exception.py",
> line 35, in inner
> response = get_response(request)
>   File "/usr/lib/python3.6/site-packages/django/core/handlers/base.py",
> line 128, in _get_response
> response = self.process_exception_by_middleware(e, request)
>   File "/usr/lib/python3.6/site-packages/django/core/handlers/base.py",
> line 126, in _get_response
> response = wrapped_callback(request, *callback_args, **callback_kwargs)
>   File "/usr/lib/python3.6/site-packages/django/views/generic/base.py",
> line 69, in view
> return self.dispatch(request, *args, **kwargs)
>   File "/usr/lib/python3.6/site-packages/cls_authentication/decorators.py",
> line 253, in _wrapper
> return bound_func(*args, **kwargs)
>   File "/usr/lib/python3.6/site-packages/django/contrib/auth/decorators.py",
> line 20, in _wrapped_view
> if test_func(request.user):
> AttributeError: 'AsgiRequest' object has no attribute 'user'
>
> Below is the list of installed packages:
> aioredis (1.0.0)
> amqp (2.2.2)
> asgi-redis (1.4.3)
> asgiref (2.1.6)
> async-timeout (2.0.1)
> attrs (17.4.0)
> autobahn (18.3.1)
> Automat (0.6.0)
> billiard (3.5.0.3)
> celery (4.1.0)
> channels (2.0.2)
> channels-redis (2.1.0)
> circus (0.14.0)
> cls-authentication (1.5.2)
> constantly (15.1.0)
> daphne (2.0.4)
> Django (2.0.3)
> django-cas-client (1.4.0)
> django-celery-beat (1.1.1)
> hiredis (0.2.0)
> hyperlink (18.0.0)
> idna (2.6)
> incremental (17.5.0)
> iowait (0.2)
> kombu (4.1.0)
> msgpack (0.5.6)
> msgpack-python (0.5.6)
> multidict (4.1.0)
> people-connector (1.1.22)
> pip (9.0.1)
> psutil (5.4.3)
> pyepics (3.3.0)
> pytz (2016.4)
> PyYAML (3.12)
> pyzmq (17.0.0)
> redis (2.10.6)
> requests (2.11.1)
> setuptools (28.8.0)
> six (1.11.0)
> tornado (4.5.3)
> Twisted (17.9.0)
> txaio (2.9.0)
> vcrpy (1.11.1)
> vine (1.1.4)
> wrapt (1.10.11)
> yarl (1.1.1)
> zope.interface (4.4.3)
>
> Thanks in advance.
>
> G Broten
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit https://groups.google.com/d/
> msgid/django-users/1b95bb8d-d5c6-4dec-ba02-746777d26318%40googlegroups.com
> 
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAFwN1ur%3Dq3oUJiX_b9fSvVQiT0LgeJRp0na5veeV6BnwD5Du3Q%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Is anyone using Channels 2 and asgi_rabbitmq?

2018-03-19 Thread Andrew Godwin
Hi Filbery,

asgi_rabbitmq has not been ported to work with the new asynchronous channel
layer API in Channels 2 yet, so it's not compatible, I'm afraid, and nobody
is currently working on that port as far as I know.

Andrew

On Mon, Mar 19, 2018 at 8:13 AM, Filbert  wrote:

> Making a major platform decision a ways out from product release. Running
> Channels 1.0 and Redis now, but we'd rather use RabbitMQ since our cluster
> is already provisioned with that for Celery, etc.
>
> I'd like to migrate to Channels 2.0 and asgi_rabbitmq, but I can't seem to
> find anyone that is using this in production.  I really don't want to use
> Redis, but I will if RabbitMQ isn't heading to first class citizen status
> in the Channels stack.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit https://groups.google.com/d/
> msgid/django-users/1d4e3641-94c7-4384-89ec-8e9e5ff58c73%40googlegroups.com
> 
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAFwN1uo7%2BzA_Sf_YpyQd1bDWc0PDY1gYFfOBKJPPvesuO%3DBVyw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: separate http requests between channels routing and "classic django routing

2018-03-14 Thread Andrew Godwin
Ah, yes, there is a bug with URLRouter and path() where it will auto-add a
$ to the regex it generates as it doesn't think the right-hand side is an
include. This is likely something I need to patch in Django itself, but
until then I suggest using re_path in any case where you want to match just
a prefix.

Andrew

On Wed, Mar 14, 2018 at 2:13 AM, Kevin Tewouda <lewou...@gmail.com> wrote:

> Thanks Andrew
> i found the mistake myself, my routing file was like this
>
> http_urlpatterns = [
> path('stream', ServerSentEventsConsumer),
> path('', AsgiHandler)
> ]
>
>
> I think that the second route will match all the default routes, but like
> it is mentionned in the tutorial, we have to use a *regex *path. So i
> change it to this and it works nice!
>
> http_urlpatterns = [
> path('stream', ServerSentEventsConsumer),
> re_path(r'', AsgiHandler)
> ]
>
>
> I think there should be a warning in the tutorial to take particular
> attention with the *path* method introduced in django 2.
>
> Thanks again.
>
> Le mercredi 14 mars 2018 05:19:28 UTC+1, Andrew Godwin a écrit :
>>
>> You can just use channels.http.AsgiHandler as the consumer/ASGI app to
>> hand off to Django views. It's mentioned in the last paragraph here:
>> http://channels.readthedocs.io/en/latest/topics/
>> routing.html#protocoltyperouter
>>
>> Andrew
>>
>> On Tue, Mar 13, 2018 at 9:20 AM, Kevin Tewouda <lewo...@gmail.com> wrote:
>>
>>> Hello,
>>> i am developing an application (a REST one) which have to serve some sse
>>> events to a web application. I saw in the latest documentation how to
>>> create an SSE consumer, but now when i create an http routing for
>>> consumers, i can't serve the others requests using the "classic django view
>>> system". So my question is how can i separate the sse routing from the
>>> other http requests to have all of this working properly.
>>> Thanks in advance for your suggestions.
>>>
>>> Best regards
>>>
>>> --
>>> You received this message because you are subscribed to the Google
>>> Groups "Django users" group.
>>> To unsubscribe from this group and stop receiving emails from it, send
>>> an email to django-users...@googlegroups.com.
>>> To post to this group, send email to django...@googlegroups.com.
>>> Visit this group at https://groups.google.com/group/django-users.
>>> To view this discussion on the web visit https://groups.google.com/d/ms
>>> gid/django-users/6b19b58f-d5e3-4e31-908b-a9371e31b2cc%40googlegroups.com
>>> <https://groups.google.com/d/msgid/django-users/6b19b58f-d5e3-4e31-908b-a9371e31b2cc%40googlegroups.com?utm_medium=email_source=footer>
>>> .
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>>
>> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit https://groups.google.com/d/
> msgid/django-users/7eaa6d7f-d088-4797-af61-dc6f3502136d%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/7eaa6d7f-d088-4797-af61-dc6f3502136d%40googlegroups.com?utm_medium=email_source=footer>
> .
>
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAFwN1ur2AtqnEWiqPtDHU84qNyrvATZ2kUFzFpQDz5KBPKBurw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: separate http requests between channels routing and "classic django routing

2018-03-13 Thread Andrew Godwin
You can just use channels.http.AsgiHandler as the consumer/ASGI app to hand
off to Django views. It's mentioned in the last paragraph here:
http://channels.readthedocs.io/en/latest/topics/routing.html#protocoltyperouter

Andrew

On Tue, Mar 13, 2018 at 9:20 AM, Kevin Tewouda  wrote:

> Hello,
> i am developing an application (a REST one) which have to serve some sse
> events to a web application. I saw in the latest documentation how to
> create an SSE consumer, but now when i create an http routing for
> consumers, i can't serve the others requests using the "classic django view
> system". So my question is how can i separate the sse routing from the
> other http requests to have all of this working properly.
> Thanks in advance for your suggestions.
>
> Best regards
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit https://groups.google.com/d/
> msgid/django-users/6b19b58f-d5e3-4e31-908b-a9371e31b2cc%40googlegroups.com
> 
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAFwN1urnqpo%2Bh122rkNLNBYA-o%3D35LWhvnHKx%2BRDYTdWROHt-A%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Listening to a django channel from outside a consumer

2018-03-13 Thread Andrew Godwin
Hi Alex,

Consumers aren't designed to be run outside of a Channels or ASGI
environment. If you read the ASGI spec (
https://github.com/django/asgiref/blob/master/specs/asgi.rst), you'll see
that to run a consumer - which is an ASGI app - you need to do quite a bit
more than you did there, in particular making an event loop and running its
coroutine.

Instead, if you want to receive on a channel layer channel externally, I
recommend calling channel_layer.receive directly. It takes a channel name
to receive on and blocks until there's a message. Ideally you should do
this all within an async context rather than using async_to_sync, but I
believe both should work.

Andrew

On Tue, Mar 13, 2018 at 12:15 AM, Alex  wrote:

> Hi,
>
>
> The docs explain that it is possible to publish to the channel layer from
> outside of a consumer: https://channels.readthedocs.io/en/latest/
> topics/channel_layers.html#using-outside-of-consumers I need to do the
> opposite. I have a fairly complex python script that reads live data from
> pubnub, processes it, and pushes it to consumers via groups on the
> channel_layer. This works fine, but I need consumers to be able to announce
> their presence to this script so that it can push them data (it currently
> pushes to the channel layer only when it gets new data from pubnub, which
> could be every 24 hours).
>
>
> I've decided to solve this by having the consumers publish to a 'presence'
> channel on connect. I now need the pubnub source script to listen to this
> channel. I've tried adding the below to the script, and it no longer throws
> errors, but it doesn't actually respond to messages. It successfully joins
> the channel layer, but the message handler 'receive_json', never fires.
>
>
> This is the class, and then the initiation further down in the code:
> from channels.generic.websocket import JsonWebsocketConsumer
>
>
> class channelConsumer(JsonWebsocketConsumer):
>
> def __init__(self):
> self.channel_name = 'source'
> def join(self):
> async_to_sync(channel_layer.group_add)('presence', self.
> channel_name)
> def receive_json(self, message):
> print("Presence Detected")
> # do some stuff
>
> global channel_layer
> channel_layer = get_channel_layer()
>
> global listener
> listener = channelConsumer()
> listener.join()
>
>
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit https://groups.google.com/d/
> msgid/django-users/08086bed-67a8-4fd6-8eec-0c417423ea8b%40googlegroups.com
> 
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAFwN1ur6Dxa%3D6Dfw2PVBKURa55xPxrDRgPa7i6SkKsDbHdrXqw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Printing to console from async method inside django channels

2018-03-09 Thread Andrew Godwin
Hi Chris,

Since you also posted this to GitHub (
https://github.com/django/channels/issues/967), I will answer there.

Andrew

On Thu, Mar 8, 2018 at 8:17 PM, Chris Barry  wrote:

> I'm trying to print inside these methods in from this example in django
> channels:
>
> https://github.com/andrewgodwin/channels-examples/blob/master/
> multichat/chat/consumers.py
>
> But nothing is coming through to the console.
>
> I haven't worked with python Async before, so I'm not sure if I'm doing
> something really stupid? The examples in the python docs seem to show
> prints working no problem
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit https://groups.google.com/d/
> msgid/django-users/abc849e2-11fe-49ca-a936-7dfaf5b811b6%40googlegroups.com
> 
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAFwN1uqNjFOcPwO4F%3D8PodCiVd6ApQVxbtDbFyW%2BWqfR7AGw1w%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: How do I transmit any changes to databases(i.e. creates and updates pertaining to a particular user) using Django Channels?

2018-03-08 Thread Andrew Godwin
Hi Alexis,

You'll need to do some work yourself - hook into database changes (either
via signals or overriding the save method) to send change notifications to
a different Group for each user (maybe use group names like "user-123213",
where the number is their ID).

Then, when someone connects via a socket or long-poll, add their consumer
to the group and listen for the change message. You can see some docs on
this here:
http://channels.readthedocs.io/en/latest/topics/channel_layers.html and an
example of sending messages to groups (albeit from another consumer rather
than a save method) here:
https://github.com/andrewgodwin/channels-examples/tree/master/multichat

Andrew

On Thu, Mar 8, 2018 at 7:35 AM, Alexis Candelaria <
alexiscandelar...@gmail.com> wrote:

> My goal is to send send any database updates to the relevant users. I was
> thinking of utilizing the views of the post and patch requests to do this
> but am not sure if there is better or more documented way.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit https://groups.google.com/d/
> msgid/django-users/b8810aad-de6b-4fb3-b1b6-06547d4e2d6a%40googlegroups.com
> 
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAFwN1uraY%3DFpWjSHy8P-UrLNWVbFAKjnFr2S5erVdNL%2B7%3DZ9Xw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Django Channels AuthMiddleware question

2018-03-04 Thread Andrew Godwin
This is a problem with _any_ ASGI app, not just middleware - doing blocking
things in the __init__ is bad.

I will look at the docs when I look at solving the SessionMiddleware
problem as well, as the two are linked.

Andrew

On Sun, Mar 4, 2018 at 7:10 AM, Hugo Castilho <hcasti...@gmail.com> wrote:

>
>
> The example in the Channels documentation shows it being used with a
> consumer called consumers.AsyncChatConsumer which suggests it's safe to use
> with async consumers.
> There probably should be a note there that it's currently not safe to use
> with async consumers no?
>
> Regarding the SessionMiddleware wouldn't this be part a more general
> problem with routers? As it is they currently have to be async "safe".
>
>
> On Sunday, March 4, 2018 at 3:03:05 AM UTC, Andrew Godwin wrote:
>>
>> Hi Hugo,
>>
>> The AuthMiddleware, like its Django counterpart, does not query the auth
>> backend immediately - it is lazily done the first time you access the
>> scope["user"] object. This does present a risk that you could access it
>> during an async method body, though - I will look into how we could fix
>> this (it might mean making it non-lazy, but I don't quite know yet).
>>
>> The SessionMiddleware does have the problem you suggest, though - I have
>> opened this issue to track it: https://github.com/django/
>> channels/issues/949
>>
>> Andrew
>>
>> On Sat, Mar 3, 2018 at 5:49 PM, Hugo Castilho <hcas...@gmail.com> wrote:
>>
>>>
>>> Hi all,
>>>
>>> The channels authentication documentation (https://channels.readthedocs.
>>> io/en/latest/topics/authentication.html) shows using the channels
>>> AuthMiddleware with an AsyncConsumer.
>>>
>>> Now, my problem is that all session backends in Django are blocking.
>>> I know that the user is only retrieved lazily but from what I understand
>>> this only means that instead of the blocking call happening during the
>>> instantiation of the application in daphne.Server.create_application it
>>> will happen in the AsyncConsumer.
>>> Either way the call will be made in the event loop.
>>> Are we not stopping the event loop for the first time every connection
>>> accesses request.user?
>>> I'm sure I'm missing something here.
>>>
>>> Thanks!
>>>
>>>
>>> --
>>> You received this message because you are subscribed to the Google
>>> Groups "Django users" group.
>>> To unsubscribe from this group and stop receiving emails from it, send
>>> an email to django-users...@googlegroups.com.
>>> To post to this group, send email to django...@googlegroups.com.
>>> Visit this group at https://groups.google.com/group/django-users.
>>> To view this discussion on the web visit https://groups.google.com/d/ms
>>> gid/django-users/f9a7ec05-7d54-4459-a08a-20d6a931c8a3%40googlegroups.com
>>> <https://groups.google.com/d/msgid/django-users/f9a7ec05-7d54-4459-a08a-20d6a931c8a3%40googlegroups.com?utm_medium=email_source=footer>
>>> .
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>>
>> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit https://groups.google.com/d/
> msgid/django-users/430b86ac-d0d7-4d50-b32d-89f2bd117b0a%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/430b86ac-d0d7-4d50-b32d-89f2bd117b0a%40googlegroups.com?utm_medium=email_source=footer>
> .
>
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAFwN1urLP41zbpooSq7RWcXbK1qaiJXbOR_znuNfu9uzT7eBgw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Django Channels AuthMiddleware question

2018-03-03 Thread Andrew Godwin
Hi Hugo,

The AuthMiddleware, like its Django counterpart, does not query the auth
backend immediately - it is lazily done the first time you access the
scope["user"] object. This does present a risk that you could access it
during an async method body, though - I will look into how we could fix
this (it might mean making it non-lazy, but I don't quite know yet).

The SessionMiddleware does have the problem you suggest, though - I have
opened this issue to track it: https://github.com/django/channels/issues/949

Andrew

On Sat, Mar 3, 2018 at 5:49 PM, Hugo Castilho  wrote:

>
> Hi all,
>
> The channels authentication documentation (https://channels.readthedocs.
> io/en/latest/topics/authentication.html) shows using the channels
> AuthMiddleware with an AsyncConsumer.
>
> Now, my problem is that all session backends in Django are blocking.
> I know that the user is only retrieved lazily but from what I understand
> this only means that instead of the blocking call happening during the
> instantiation of the application in daphne.Server.create_application it
> will happen in the AsyncConsumer.
> Either way the call will be made in the event loop.
> Are we not stopping the event loop for the first time every connection
> accesses request.user?
> I'm sure I'm missing something here.
>
> Thanks!
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit https://groups.google.com/d/
> msgid/django-users/f9a7ec05-7d54-4459-a08a-20d6a931c8a3%40googlegroups.com
> 
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAFwN1uoC97nwCEsCX5kyVHZuzv2qFWdsf8JotGq7et5QM0kaGQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Channels 2: Consumer running twice when two users simultaneously share socket

2018-03-01 Thread Andrew Godwin
The problem appears to be that you are saving to the database inside the
chat_message handler - since you're sending a message of type
"chat.message" to the group, that means everything in the group is going to
receive the message, run that handler, and save (so with 2 connected you
get 2 saves, etc.)

I'd recommend saving instead where you send to the group.

Andrew

On Thu, Mar 1, 2018 at 10:02 PM, lakeshow <sjung2...@gmail.com> wrote:

> Sure. The JS code is essentially identical to your example. By "two users
> connected", I simply meant two users have clicked to join in a chatroom by
> way of `socket.send()`.
>
> I've tested it by using two different browsers and having users log in to
> join the same room. When they are both "connected"(in the same room) and
> chatting to each other, any message that either party sends will be
> broadcast only once to the chatroom(as it should be), but save twice into
> the database--two copies of a message will be saved.
>
> channels==2.0.2
>
> channels-redis==2.1.0
>
> Django==1.11.8
>
> redis==2.10.6
>
> asgi-redis==1.4.3
>
> asgiref==2.1.6
>
> daphne==2.0.4
>
> I'm realizing now that I've a lot of left-over installs since upgrading
> from Channels 1.x. I'm no longer even sure which I need. Could this be the
> issue?
>
>
> On Friday, March 2, 2018 at 6:49:41 AM UTC+9, Andrew Godwin wrote:
>>
>> Can you clarify what you mean by "two users connected to the same
>> socket"? If you have two websockets connected, you'll have two copies of
>> your consumer running, one for each socket.
>>
>> Andrew
>>
>> On Thu, Mar 1, 2018 at 7:52 AM, lakeshow <sjun...@gmail.com> wrote:
>>
>>> I am running into an issue using Channels where if two users are
>>> connected to the same socket, the consumer will run twice. When a single
>>> user is connected, only once.
>>> So in practice: when I have two users chatting with each other over
>>> channels, a single sent message will be run through the consumer twice,
>>> causing it to save twice to the database. However, if there is only one
>>> participant in the channel, the message will run through the consumer once
>>> and save once as expected.
>>>
>>> What might be the issue?
>>>
>>> Here is the consumer:
>>>
>>> class ChatConsumer(AsyncJsonWebsocketConsumer):
>>> ...
>>> async def receive_json(self, content):
>>> """
>>> Called when we get a text frame. Channels will JSON-decode the 
>>> payload
>>> for us and pass it as the first argument.
>>> """
>>> # Messages will have a "command" key we can switch on
>>> command = content.get("command", None)
>>> recipient = content.get("recipient", None)
>>> sender = self.scope["user"]
>>> try:
>>> if command == "join":
>>> # Make them join the room
>>> await self.join_room(content["room"])
>>> previous_message_list = await get_saved_messages(recipient, 
>>> sender)
>>> for msg in previous_message_list:
>>> await self.send_json({
>>> "msg_type": MSG_TYPE_MESSAGE,
>>> "room": content["room"],
>>> "username": msg.sender.user.username,
>>> "message": msg.message,
>>> },)
>>> elif command == "leave":
>>> # Leave the room
>>> await self.leave_room(content["room"])
>>> elif command == "send":
>>> await self.send_room(
>>> content["room"],
>>> content["message"],
>>> content["recipient"]
>>> )
>>> except ClientError as e:
>>> # Catch any errors and send it back
>>> await self.send_json({"error": e.code})
>>> ...
>>> async def send_room(self, room_id, message, recipient):
>>> """
>>> Called by receive_json when someone sends a message to a room.
>>> """
>>> # Check they ar

Re: Channels 2: Consumer running twice when two users simultaneously share socket

2018-03-01 Thread Andrew Godwin
Can you clarify what you mean by "two users connected to the same socket"?
If you have two websockets connected, you'll have two copies of your
consumer running, one for each socket.

Andrew

On Thu, Mar 1, 2018 at 7:52 AM, lakeshow  wrote:

> I am running into an issue using Channels where if two users are connected
> to the same socket, the consumer will run twice. When a single user is
> connected, only once.
> So in practice: when I have two users chatting with each other over
> channels, a single sent message will be run through the consumer twice,
> causing it to save twice to the database. However, if there is only one
> participant in the channel, the message will run through the consumer once
> and save once as expected.
>
> What might be the issue?
>
> Here is the consumer:
>
> class ChatConsumer(AsyncJsonWebsocketConsumer):
> ...
> async def receive_json(self, content):
> """
> Called when we get a text frame. Channels will JSON-decode the payload
> for us and pass it as the first argument.
> """
> # Messages will have a "command" key we can switch on
> command = content.get("command", None)
> recipient = content.get("recipient", None)
> sender = self.scope["user"]
> try:
> if command == "join":
> # Make them join the room
> await self.join_room(content["room"])
> previous_message_list = await get_saved_messages(recipient, 
> sender)
> for msg in previous_message_list:
> await self.send_json({
> "msg_type": MSG_TYPE_MESSAGE,
> "room": content["room"],
> "username": msg.sender.user.username,
> "message": msg.message,
> },)
> elif command == "leave":
> # Leave the room
> await self.leave_room(content["room"])
> elif command == "send":
> await self.send_room(
> content["room"],
> content["message"],
> content["recipient"]
> )
> except ClientError as e:
> # Catch any errors and send it back
> await self.send_json({"error": e.code})
> ...
> async def send_room(self, room_id, message, recipient):
> """
> Called by receive_json when someone sends a message to a room.
> """
> # Check they are in this room
> if room_id not in self.rooms:
> raise ClientError("ROOM_ACCESS_DENIED")
> # Get the room and send to the group about it
> room = await get_room_or_error(room_id, self.scope["user"])
> await self.channel_layer.group_send(
> room.group_name,
> {
> "type": "chat.message",
> "room_id": room_id,
> "username": self.scope["user"].username,
> "message": message,
> "recipient": recipient,
> }
> )
> ...
> async def chat_message(self, event):
> """
> Called when someone has messaged our chat.
> """
> # Send a message down to the client
> # Save message object
> await save_message_object(
> sender=event["username"],
> message=event["message"],
> recipient=event["recipient"]
> )
> await self.send_json(
> {
> "msg_type": MSG_TYPE_MESSAGE,
> "room": event["room_id"],
> "username": event["username"],
> "message": event["message"],
> },
> )
>
>
> So something is causing the last function--def chat_message() to run twice
> and I think it's in the consumer and not related to the client because sent
> messages will log to the console only once--as soon as the messages are
> sent over socket.send, there will be two of the same Message objects saved
> onto the db.
>
> Here is code relating to the actual save. It is in utils.py and is
> decorated using sync_to_async in order to call the sync func from an async
> consumer:
>
> @database_sync_to_asyncdef save_message_object(sender, message, recipient):
> recipient = Profile.objects.get(user__username=recipient)
> sender = Profile.objects.get(user__username=sender)
> m = Message(sender=sender, message=message, recipient=recipient)
> m.save()
> print(datetime.datetime.now())
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion 

  1   2   3   4   >