What is the proper way to process the user-trigger events in Django?
For example, I have a blog based on Django
and I already have several functions for users: login
、edit_profile
、share
.
But now I need to implement a mission system.
- User logins, reward
10 score
per day - User completes his profile, reward
20 score
- 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
add a comment |
For example, I have a blog based on Django
and I already have several functions for users: login
、edit_profile
、share
.
But now I need to implement a mission system.
- User logins, reward
10 score
per day - User completes his profile, reward
20 score
- 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
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 usepost_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 knowsignal
. But it is synchronous and that is why I mentioneddjango-q
.
– Yriuns
Nov 23 '18 at 9:07
add a comment |
For example, I have a blog based on Django
and I already have several functions for users: login
、edit_profile
、share
.
But now I need to implement a mission system.
- User logins, reward
10 score
per day - User completes his profile, reward
20 score
- 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
For example, I have a blog based on Django
and I already have several functions for users: login
、edit_profile
、share
.
But now I need to implement a mission system.
- User logins, reward
10 score
per day - User completes his profile, reward
20 score
- 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
python django message-queue
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 usepost_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 knowsignal
. But it is synchronous and that is why I mentioneddjango-q
.
– Yriuns
Nov 23 '18 at 9:07
add a comment |
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 usepost_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 knowsignal
. But it is synchronous and that is why I mentioneddjango-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
add a comment |
3 Answers
3
active
oldest
votes
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...
add a comment |
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.
add a comment |
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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...
add a comment |
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...
add a comment |
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...
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...
edited Nov 29 '18 at 8:09
answered Nov 29 '18 at 7:19
Incognos
1,6101021
1,6101021
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 23 '18 at 18:45
Srivats Shankar
36135
36135
add a comment |
add a comment |
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
add a comment |
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
add a comment |
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
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
edited Nov 29 '18 at 7:10
answered Nov 28 '18 at 12:36
subramanyam
513
513
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 mentioneddjango-q
.– Yriuns
Nov 23 '18 at 9:07