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

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

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

Rithwik Cherian
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 [hidden email].
To post to this group, send email to [hidden email].
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.
Reply | Threaded
Open this post in threaded view
|

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

Ryan Nowakowski
I've never used pytransitions. Typically persistent state in Django is stored in the model instead of objects in memory. This allows you to easily scale and also recover state if your app process stops.

On June 9, 2018 1:01:14 AM CDT, Rithwik Cherian <[hidden email]> 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 [hidden email].
To post to this group, send email to [hidden email].
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/9898A95D-95E1-453A-8392-B2D90F9B7D4A%40fattuba.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

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

Andrew Godwin-3
In reply to this post by Rithwik Cherian
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 <[hidden email]> 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 [hidden email].
To post to this group, send email to [hidden email].
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 [hidden email].
To post to this group, send email to [hidden email].
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.
Reply | Threaded
Open this post in threaded view
|

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

Rithwik Cherian
In reply to this post by Ryan Nowakowski
Hey Ryan. 
The problem is actually not specific to pytransitions. But anyways, I wanted to persist an object in memory and not have to retrieve it from the DB every time. Although I'm not sure which is better for scaling (please give suggestions if you know more). As for recovering state, I'm planning to store the state in a session variable as a fail safe.

On Sunday, June 10, 2018 at 7:07:23 AM UTC+5:30, Ryan Nowakowski wrote:
I've never used pytransitions. Typically persistent state in Django is stored in the model instead of objects in memory. This allows you to easily scale and also recover state if your app process stops.

On June 9, 2018 1:01:14 AM CDT, Rithwik Cherian <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="aJs9_uJHBQAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">rhk...@...> 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 (<a href="https://github.com/pytransitions/transitions" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fgithub.com%2Fpytransitions%2Ftransitions\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHYkNpNXMNTMhT8ojMNoz-Orip09A&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fgithub.com%2Fpytransitions%2Ftransitions\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHYkNpNXMNTMhT8ojMNoz-Orip09A&#39;;return true;">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 [hidden email].
To post to this group, send email to [hidden email].
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/cc8f52ad-4874-4345-a8e4-905b63f42f39%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

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

Rithwik Cherian
In reply to this post by Andrew Godwin-3
Hi Andrew,

Great. That's good to know. :) 
I'll try it out right away.
Yeah, I read somewhere that it is available from Channels 2. I am using the latest version anyways. :)

Well, pardon my usage. I am not 100% sure about what exactly these terms (websocket, asgi etc) mean exactly (I'm very new to all this), so I may have made a few mistakes when talking about them.
As for what happens when connection is lost or reconnects, I'm planning to store the state in a session variable as well to be safe.

On Sunday, June 10, 2018 at 10:14:29 PM UTC+5:30, Andrew Godwin wrote:
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 <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="ct-H7GJ5BQAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">rhk...@...> 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 (<a href="https://github.com/pytransitions/transitions" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fgithub.com%2Fpytransitions%2Ftransitions\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHYkNpNXMNTMhT8ojMNoz-Orip09A&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fgithub.com%2Fpytransitions%2Ftransitions\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHYkNpNXMNTMhT8ojMNoz-Orip09A&#39;;return true;">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 <a href="javascript:" target="_blank" gdf-obfuscated-mailto="ct-H7GJ5BQAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">django-users...@googlegroups.com.
To post to this group, send email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="ct-H7GJ5BQAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">django...@....
Visit this group at <a href="https://groups.google.com/group/django-users" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/group/django-users&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/group/django-users&#39;;return true;">https://groups.google.com/group/django-users.
To view this discussion on the web visit <a href="https://groups.google.com/d/msgid/django-users/142cb856-17f7-4cf1-9f3b-87d80e0ca5f8%40googlegroups.com?utm_medium=email&amp;utm_source=footer" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/django-users/142cb856-17f7-4cf1-9f3b-87d80e0ca5f8%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/django-users/142cb856-17f7-4cf1-9f3b-87d80e0ca5f8%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;">https://groups.google.com/d/msgid/django-users/142cb856-17f7-4cf1-9f3b-87d80e0ca5f8%40googlegroups.com.
For more options, visit <a href="https://groups.google.com/d/optout" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;">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 [hidden email].
To post to this group, send email to [hidden email].
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/0a2dc931-cd92-43ab-a6ee-9536dd018f9f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.