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 8627179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:12:44+00:00 2026-06-12T08:12:44+00:00

I followed a queue example from the bottom of the Python Queue page. I

  • 0

I followed a queue example from the bottom of the Python Queue page. I want to access some global variables within the worker function, however, some globals are accessible and some aren’t. In my simple example below, the ‘rows’ and ‘errors’ variables are accessible but then I get UnboundLocalError for count. If i put “global count” then it works. But I don’t understand why the other globals are accessible.

File "myfile.py", line 184, in dpn_worker
    count += 1
UnboundLocalError: local variable 'count' referenced before assignment

Here is the code example I used:

dpns = [1,2,3,4]
q = Queue.Queue()
rows = []
errors = []
count = 0

def dpn_worker():
    while True:
        item = q.get()
        rows.append(1)
        errors.append(1)
        count += 1
        q.task_done()

def main():
    for d in dpns:
        q.put(d)

    for i in range(NUM_WORKERS):
        t = threading.Thread(target=dpn_worker)
        t.daemon = True
        t.start()

    q.join()
  • 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-12T08:12:45+00:00Added an answer on June 12, 2026 at 8:12 am

    In this case, since count is an integer: –

    count += 1
    

    is equivalent to

    count = count + 1
    

    So, you are trying to modify the global variable, but are actually creating a local one.. But on the RHS, you are using the local variable before initialization..

    Modify your dpn_worker as: –

    def dpn_worker():
        global count
        while True:
            item = q.get()
            rows.append(1)
            errors.append(1)
            count += 1
            q.task_done()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to know the best practices followed to share a queue (resource) between
Followed this example on telerik's website and implemented as follows:- aspx page:- <div id=div1
followed the docs, tried my own and bump into some problem. initializeViewModel = function(){
I followed this tutorial to implement facebook into my application. All I want is
I followed the example given in ember application stucture guide http://emberjs.com/guides/outlets/ in order to
I want to assign record to a particular queue in my Silverlight application. I
We have a JMS queue of job statuses, and two identical processes pulling from
I have been trying to access a queue that is on the cloud while
After talking with a friend of mine from Google, I'd like to implement some
I followed below code to establish https connection in my application to fetch some

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.