Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8400097
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:28:36+00:00 2026-06-09T21:28:36+00:00

I have a task, somewhat like this: @task() def async_work(info): … At any moment,

  • 0

I have a task, somewhat like this:

@task()
def async_work(info):
    ...

At any moment, I may call async_work with some info. For some reason, I need to make sure that only one async_work is running at a time, other calling request must wait for.

So I come up with the following code:

is_locked = False    
@task()
def async_work(info):
    while is_locked:
        pass
    is_locked = True
    ...
    is_locked = False

But it says it’s invalid to access local variables…
How to solve it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T21:28:38+00:00Added an answer on June 9, 2026 at 9:28 pm

    It is invalid to access local variables since you can have several celery workers running tasks. And those workers might even be on different hosts. So, basically, there is as many is_locked variable instances as many Celery workers are running
    your async_work task. Thus, even though your code won’t raise any errors you wouldn’t get desired effect with it.

    To achieve you goal you need to configure Celery to run only one worker. Since any worker can process a single task at any given time you get what you need.

    EDIT:

    According to Workers Guide > Concurrency:

    By default multiprocessing is used to perform concurrent execution of
    tasks, but you can also use Eventlet. The number of worker
    processes/threads can be changed using the --concurrency argument
    and defaults to the number of CPUs available on the machine.

    Thus you need to run the worker like this:

    $ celery worker --concurrency=1
    

    EDIT 2:

    Surprisingly there’s another solution, moreover it is even in the official docs, see the Ensuring a task is only executed one at a time article.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have code that's somewhat like this: ($i=0; $i < 100; $i++) { do
My question somewhat overlaps with this and several other similar ones. Those have some
I have a text file, that is formatted somewhat like this: 1 3 4
I am new this sharepoint development and i have task in hand to do
I have some task regarding do operations on database using ADODB in Vb.NET. Can
I have the task to integrate some commercial sharing stuff into a website. The
I have a task that runs periodically 10 second. I do some picturebox refreshing
The task I have is to (somewhat efficiently) read line-by-line through a very large,
I have an ASCII log file with some content I would like to extract.
So I have a rake file like the following: require 'fileutils' task :copy do

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.