What is the proper way to process the user-trigger events in Django?












6














For example, I have a blog based on Django and I already have several functions for users: loginedit_profileshare.



But now I need to implement a mission system.




  1. User logins, reward 10 score per day

  2. User completes his profile, reward 20 score

  3. User shares my blogs, reward 30 score


I don't want to mix reward code with normal functions code. So I decide to use message queue. Pseudo code may look like:



@login_required
def edit_profile(request):
user = request.user
nickname = ...
desc = ...
user.save(...)
action.send(sender='edit_profile', payload={'user_id': user.id})
return Response(...)


And the reward can subscribe this action



@receiver('edit_profile')
def edit_profile_reward(payload):
user_id = payload['user_id']
user = User.objects.get(id=user_id)
mission, created = Mission.objects.get_or_create(user=user, type='complete_profile')
if created:
user.score += 20
user.save()


But I don't know if this is the right way. If so, what message queue should I use? django-channel / django-q or something else?
If not, what is the best practice?










share|improve this question






















  • Your sender/receiver method seems like a good idea. Probably easier to implement than the method I would use (i.e., database trigger coupled to and handled by a separate service). Have you tried to implement the method you've described? If so, what were the results?
    – Tommy
    Nov 21 '18 at 17:50










  • No I haven't. I'm still thinking about an appropriate approach, lol.
    – Yriuns
    Nov 22 '18 at 16:22










  • Have you considered Django Signals? You can use post_save to run your score logic after profile is updated. Otherwise I would start a monitoring thread upon app start (perhaps with a middleware) which would consume messages put in a queue. I would use Python's built in Queue and put messages in it in my view function.
    – Mehdi Sadeghi
    Nov 23 '18 at 8:48










  • Yes, I know signal. But it is synchronous and that is why I mentioned django-q.
    – Yriuns
    Nov 23 '18 at 9:07
















6














For example, I have a blog based on Django and I already have several functions for users: loginedit_profileshare.



But now I need to implement a mission system.




  1. User logins, reward 10 score per day

  2. User completes his profile, reward 20 score

  3. User shares my blogs, reward 30 score


I don't want to mix reward code with normal functions code. So I decide to use message queue. Pseudo code may look like:



@login_required
def edit_profile(request):
user = request.user
nickname = ...
desc = ...
user.save(...)
action.send(sender='edit_profile', payload={'user_id': user.id})
return Response(...)


And the reward can subscribe this action



@receiver('edit_profile')
def edit_profile_reward(payload):
user_id = payload['user_id']
user = User.objects.get(id=user_id)
mission, created = Mission.objects.get_or_create(user=user, type='complete_profile')
if created:
user.score += 20
user.save()


But I don't know if this is the right way. If so, what message queue should I use? django-channel / django-q or something else?
If not, what is the best practice?










share|improve this question






















  • Your sender/receiver method seems like a good idea. Probably easier to implement than the method I would use (i.e., database trigger coupled to and handled by a separate service). Have you tried to implement the method you've described? If so, what were the results?
    – Tommy
    Nov 21 '18 at 17:50










  • No I haven't. I'm still thinking about an appropriate approach, lol.
    – Yriuns
    Nov 22 '18 at 16:22










  • Have you considered Django Signals? You can use post_save to run your score logic after profile is updated. Otherwise I would start a monitoring thread upon app start (perhaps with a middleware) which would consume messages put in a queue. I would use Python's built in Queue and put messages in it in my view function.
    – Mehdi Sadeghi
    Nov 23 '18 at 8:48










  • Yes, I know signal. But it is synchronous and that is why I mentioned django-q.
    – Yriuns
    Nov 23 '18 at 9:07














6












6








6


1





For example, I have a blog based on Django and I already have several functions for users: loginedit_profileshare.



But now I need to implement a mission system.




  1. User logins, reward 10 score per day

  2. User completes his profile, reward 20 score

  3. User shares my blogs, reward 30 score


I don't want to mix reward code with normal functions code. So I decide to use message queue. Pseudo code may look like:



@login_required
def edit_profile(request):
user = request.user
nickname = ...
desc = ...
user.save(...)
action.send(sender='edit_profile', payload={'user_id': user.id})
return Response(...)


And the reward can subscribe this action



@receiver('edit_profile')
def edit_profile_reward(payload):
user_id = payload['user_id']
user = User.objects.get(id=user_id)
mission, created = Mission.objects.get_or_create(user=user, type='complete_profile')
if created:
user.score += 20
user.save()


But I don't know if this is the right way. If so, what message queue should I use? django-channel / django-q or something else?
If not, what is the best practice?










share|improve this question













For example, I have a blog based on Django and I already have several functions for users: loginedit_profileshare.



But now I need to implement a mission system.




  1. User logins, reward 10 score per day

  2. User completes his profile, reward 20 score

  3. User shares my blogs, reward 30 score


I don't want to mix reward code with normal functions code. So I decide to use message queue. Pseudo code may look like:



@login_required
def edit_profile(request):
user = request.user
nickname = ...
desc = ...
user.save(...)
action.send(sender='edit_profile', payload={'user_id': user.id})
return Response(...)


And the reward can subscribe this action



@receiver('edit_profile')
def edit_profile_reward(payload):
user_id = payload['user_id']
user = User.objects.get(id=user_id)
mission, created = Mission.objects.get_or_create(user=user, type='complete_profile')
if created:
user.score += 20
user.save()


But I don't know if this is the right way. If so, what message queue should I use? django-channel / django-q or something else?
If not, what is the best practice?







python django message-queue






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 '18 at 6:38









Yriuns

301316




301316












  • Your sender/receiver method seems like a good idea. Probably easier to implement than the method I would use (i.e., database trigger coupled to and handled by a separate service). Have you tried to implement the method you've described? If so, what were the results?
    – Tommy
    Nov 21 '18 at 17:50










  • No I haven't. I'm still thinking about an appropriate approach, lol.
    – Yriuns
    Nov 22 '18 at 16:22










  • Have you considered Django Signals? You can use post_save to run your score logic after profile is updated. Otherwise I would start a monitoring thread upon app start (perhaps with a middleware) which would consume messages put in a queue. I would use Python's built in Queue and put messages in it in my view function.
    – Mehdi Sadeghi
    Nov 23 '18 at 8:48










  • Yes, I know signal. But it is synchronous and that is why I mentioned django-q.
    – Yriuns
    Nov 23 '18 at 9:07


















  • Your sender/receiver method seems like a good idea. Probably easier to implement than the method I would use (i.e., database trigger coupled to and handled by a separate service). Have you tried to implement the method you've described? If so, what were the results?
    – Tommy
    Nov 21 '18 at 17:50










  • No I haven't. I'm still thinking about an appropriate approach, lol.
    – Yriuns
    Nov 22 '18 at 16:22










  • Have you considered Django Signals? You can use post_save to run your score logic after profile is updated. Otherwise I would start a monitoring thread upon app start (perhaps with a middleware) which would consume messages put in a queue. I would use Python's built in Queue and put messages in it in my view function.
    – Mehdi Sadeghi
    Nov 23 '18 at 8:48










  • Yes, I know signal. But it is synchronous and that is why I mentioned django-q.
    – Yriuns
    Nov 23 '18 at 9:07
















Your sender/receiver method seems like a good idea. Probably easier to implement than the method I would use (i.e., database trigger coupled to and handled by a separate service). Have you tried to implement the method you've described? If so, what were the results?
– Tommy
Nov 21 '18 at 17:50




Your sender/receiver method seems like a good idea. Probably easier to implement than the method I would use (i.e., database trigger coupled to and handled by a separate service). Have you tried to implement the method you've described? If so, what were the results?
– Tommy
Nov 21 '18 at 17:50












No I haven't. I'm still thinking about an appropriate approach, lol.
– Yriuns
Nov 22 '18 at 16:22




No I haven't. I'm still thinking about an appropriate approach, lol.
– Yriuns
Nov 22 '18 at 16:22












Have you considered Django Signals? You can use post_save to run your score logic after profile is updated. Otherwise I would start a monitoring thread upon app start (perhaps with a middleware) which would consume messages put in a queue. I would use Python's built in Queue and put messages in it in my view function.
– Mehdi Sadeghi
Nov 23 '18 at 8:48




Have you considered Django Signals? You can use post_save to run your score logic after profile is updated. Otherwise I would start a monitoring thread upon app start (perhaps with a middleware) which would consume messages put in a queue. I would use Python's built in Queue and put messages in it in my view function.
– Mehdi Sadeghi
Nov 23 '18 at 8:48












Yes, I know signal. But it is synchronous and that is why I mentioned django-q.
– Yriuns
Nov 23 '18 at 9:07




Yes, I know signal. But it is synchronous and that is why I mentioned django-q.
– Yriuns
Nov 23 '18 at 9:07












3 Answers
3






active

oldest

votes


















0





+50









If you are looking for Async queue, you will need a combo of Redis and workers.



One of the most common libraries, and simplest, out there for this is RQ Workers



Implementation is simple, but you will need to run the rq-workers as a separate app.



It also allows you to implement different queues with different priorities. I use these for things like sending emails or things that need to be updated without making the user wait (logs, etc...)



Django-Q is another good solution with the advantage of being able to use your current database as the queue - but also works with Redis et al...



Finally, Celery is the grandaddy of them all. You can have scheduled jobs with Celeray as well as async jobs. A bit more complex but good solution.



Hope this helps...






share|improve this answer































    0














    What you are seeking to do is fairly normal when it comes to cuing tasks with Django or any Python framework for that matter. While there is no "right" way to do this, I personally would recommend going with Redis. Considering that you would have many users receiving points this would make your querying really fast.



    You can naturally makes this up with Celery to make your own Stack. Everything will be done in RAM, which will be helpful for such repetitive tasks.



    You can take a look at Redis for Django over here.



    You would essentially need to include this as a caching server in your settings.



    In whichever file you implement cuing, remember to add the following:



    from django.core.cache.backends.base import DEFAULT_TIMEOUT
    from django.views.decorators.cache import cache_page


    I would agree that initially setting this up seems daunting, but trust me on this it is a great way to cue any task quickly and efficiently. Give it a shot! You will find it extremely useful in all of your projects.






    share|improve this answer





























      0














      For asynchronous/deferred execution of tasks/jobs you can use



      Celery: https://github.com/celery/celery/



      Django:
      http://docs.celeryproject.org/en/latest/django/first-steps-with-django.html






      share|improve this answer























        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53406489%2fwhat-is-the-proper-way-to-process-the-user-trigger-events-in-django%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        0





        +50









        If you are looking for Async queue, you will need a combo of Redis and workers.



        One of the most common libraries, and simplest, out there for this is RQ Workers



        Implementation is simple, but you will need to run the rq-workers as a separate app.



        It also allows you to implement different queues with different priorities. I use these for things like sending emails or things that need to be updated without making the user wait (logs, etc...)



        Django-Q is another good solution with the advantage of being able to use your current database as the queue - but also works with Redis et al...



        Finally, Celery is the grandaddy of them all. You can have scheduled jobs with Celeray as well as async jobs. A bit more complex but good solution.



        Hope this helps...






        share|improve this answer




























          0





          +50









          If you are looking for Async queue, you will need a combo of Redis and workers.



          One of the most common libraries, and simplest, out there for this is RQ Workers



          Implementation is simple, but you will need to run the rq-workers as a separate app.



          It also allows you to implement different queues with different priorities. I use these for things like sending emails or things that need to be updated without making the user wait (logs, etc...)



          Django-Q is another good solution with the advantage of being able to use your current database as the queue - but also works with Redis et al...



          Finally, Celery is the grandaddy of them all. You can have scheduled jobs with Celeray as well as async jobs. A bit more complex but good solution.



          Hope this helps...






          share|improve this answer


























            0





            +50







            0





            +50



            0




            +50




            If you are looking for Async queue, you will need a combo of Redis and workers.



            One of the most common libraries, and simplest, out there for this is RQ Workers



            Implementation is simple, but you will need to run the rq-workers as a separate app.



            It also allows you to implement different queues with different priorities. I use these for things like sending emails or things that need to be updated without making the user wait (logs, etc...)



            Django-Q is another good solution with the advantage of being able to use your current database as the queue - but also works with Redis et al...



            Finally, Celery is the grandaddy of them all. You can have scheduled jobs with Celeray as well as async jobs. A bit more complex but good solution.



            Hope this helps...






            share|improve this answer














            If you are looking for Async queue, you will need a combo of Redis and workers.



            One of the most common libraries, and simplest, out there for this is RQ Workers



            Implementation is simple, but you will need to run the rq-workers as a separate app.



            It also allows you to implement different queues with different priorities. I use these for things like sending emails or things that need to be updated without making the user wait (logs, etc...)



            Django-Q is another good solution with the advantage of being able to use your current database as the queue - but also works with Redis et al...



            Finally, Celery is the grandaddy of them all. You can have scheduled jobs with Celeray as well as async jobs. A bit more complex but good solution.



            Hope this helps...







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 29 '18 at 8:09

























            answered Nov 29 '18 at 7:19









            Incognos

            1,6101021




            1,6101021

























                0














                What you are seeking to do is fairly normal when it comes to cuing tasks with Django or any Python framework for that matter. While there is no "right" way to do this, I personally would recommend going with Redis. Considering that you would have many users receiving points this would make your querying really fast.



                You can naturally makes this up with Celery to make your own Stack. Everything will be done in RAM, which will be helpful for such repetitive tasks.



                You can take a look at Redis for Django over here.



                You would essentially need to include this as a caching server in your settings.



                In whichever file you implement cuing, remember to add the following:



                from django.core.cache.backends.base import DEFAULT_TIMEOUT
                from django.views.decorators.cache import cache_page


                I would agree that initially setting this up seems daunting, but trust me on this it is a great way to cue any task quickly and efficiently. Give it a shot! You will find it extremely useful in all of your projects.






                share|improve this answer


























                  0














                  What you are seeking to do is fairly normal when it comes to cuing tasks with Django or any Python framework for that matter. While there is no "right" way to do this, I personally would recommend going with Redis. Considering that you would have many users receiving points this would make your querying really fast.



                  You can naturally makes this up with Celery to make your own Stack. Everything will be done in RAM, which will be helpful for such repetitive tasks.



                  You can take a look at Redis for Django over here.



                  You would essentially need to include this as a caching server in your settings.



                  In whichever file you implement cuing, remember to add the following:



                  from django.core.cache.backends.base import DEFAULT_TIMEOUT
                  from django.views.decorators.cache import cache_page


                  I would agree that initially setting this up seems daunting, but trust me on this it is a great way to cue any task quickly and efficiently. Give it a shot! You will find it extremely useful in all of your projects.






                  share|improve this answer
























                    0












                    0








                    0






                    What you are seeking to do is fairly normal when it comes to cuing tasks with Django or any Python framework for that matter. While there is no "right" way to do this, I personally would recommend going with Redis. Considering that you would have many users receiving points this would make your querying really fast.



                    You can naturally makes this up with Celery to make your own Stack. Everything will be done in RAM, which will be helpful for such repetitive tasks.



                    You can take a look at Redis for Django over here.



                    You would essentially need to include this as a caching server in your settings.



                    In whichever file you implement cuing, remember to add the following:



                    from django.core.cache.backends.base import DEFAULT_TIMEOUT
                    from django.views.decorators.cache import cache_page


                    I would agree that initially setting this up seems daunting, but trust me on this it is a great way to cue any task quickly and efficiently. Give it a shot! You will find it extremely useful in all of your projects.






                    share|improve this answer












                    What you are seeking to do is fairly normal when it comes to cuing tasks with Django or any Python framework for that matter. While there is no "right" way to do this, I personally would recommend going with Redis. Considering that you would have many users receiving points this would make your querying really fast.



                    You can naturally makes this up with Celery to make your own Stack. Everything will be done in RAM, which will be helpful for such repetitive tasks.



                    You can take a look at Redis for Django over here.



                    You would essentially need to include this as a caching server in your settings.



                    In whichever file you implement cuing, remember to add the following:



                    from django.core.cache.backends.base import DEFAULT_TIMEOUT
                    from django.views.decorators.cache import cache_page


                    I would agree that initially setting this up seems daunting, but trust me on this it is a great way to cue any task quickly and efficiently. Give it a shot! You will find it extremely useful in all of your projects.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 23 '18 at 18:45









                    Srivats Shankar

                    36135




                    36135























                        0














                        For asynchronous/deferred execution of tasks/jobs you can use



                        Celery: https://github.com/celery/celery/



                        Django:
                        http://docs.celeryproject.org/en/latest/django/first-steps-with-django.html






                        share|improve this answer




























                          0














                          For asynchronous/deferred execution of tasks/jobs you can use



                          Celery: https://github.com/celery/celery/



                          Django:
                          http://docs.celeryproject.org/en/latest/django/first-steps-with-django.html






                          share|improve this answer


























                            0












                            0








                            0






                            For asynchronous/deferred execution of tasks/jobs you can use



                            Celery: https://github.com/celery/celery/



                            Django:
                            http://docs.celeryproject.org/en/latest/django/first-steps-with-django.html






                            share|improve this answer














                            For asynchronous/deferred execution of tasks/jobs you can use



                            Celery: https://github.com/celery/celery/



                            Django:
                            http://docs.celeryproject.org/en/latest/django/first-steps-with-django.html







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Nov 29 '18 at 7:10

























                            answered Nov 28 '18 at 12:36









                            subramanyam

                            513




                            513






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.





                                Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                Please pay close attention to the following guidance:


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53406489%2fwhat-is-the-proper-way-to-process-the-user-trigger-events-in-django%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                What visual should I use to simply compare current year value vs last year in Power BI desktop

                                Alexandru Averescu

                                Trompette piccolo