Question

Going from this question, I have this task:

@task()
def create_user(data):
    try:
        User.objects.get(username=data['username'])
        return 'Username already exists'
    except:
        user = User.objects.create_user(username=data['username'], email=None, password=data['password']
)
        user.save()
        profile = UserProfile()
        profile.user = user
        profile.token = generate_token()
        profile.save()

        return profile.token

The way I understand Celery works is that this task can be put in a qeue and executed after some time, assuming there a lot of requests.

Let's assume a mobile app is communicating with this server. The user sends the combination of username and password to the server to register. The server will return the token only after it has processed the task. There's no way the customer can login if it doesn't have the token. So I have to wait until the task is processed to be able to send the token. So everything becomes asynchronous processing. How does Celery solve the problem then?

Was it helpful?

Solution

celery should be used for heavy server side tasks when it is not mandatory for the user to wait for a response and not for simple task as login.

OTHER TIPS

Celery is for async processing (well, mostly.) If you are performing the operation that you describe, then you have to have the token before the user can proceed. In that case, you have to treat the call to celery as a blocking call by calling something like token = my_task.get() after you start the task. It is then a blocking call, which is bad (likely) but it is offloaded to a separate process, which can be a good thing.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top