1

Okay. So I'm trying to make 2 threads to run and increment a value so it knows when to stop. I'm kind of lost, because I'm new to Python, and everything looks right to me..

import threading;
import socket;
import time;

count = 0;

class inp(threading.Thread):
    def run(self):
        while count < 11:
            time.sleep(0.5);
            print("Thread 1!");
            count += 1;

class recv_oup(threading.Thread):
    def run(self):
        while count < 31:
            time.sleep(0.5);
            print("Thread 2!");
            count += 1;

inp().start();
recv_oup().start();

And the error is quite long...

Exception in thread Thread-1:
Traceback (most recent call last):
  File "/usr/lib/python2.7/threading.py", line 551, in __bootstrap_inner
    self.run()
  File "core.py", line 9, in run
    while count < 11:
UnboundLocalError: local variable 'count' referenced before assignment

Exception in thread Thread-2:
Traceback (most recent call last):
  File "/usr/lib/python2.7/threading.py", line 551, in __bootstrap_inner
    self.run()
  File "core.py", line 16, in run
    while count < 31:
UnboundLocalError: local variable 'count' referenced before assignment

I have no clue what's happening. As I said, new to Python, so this is all gibberish to me. Any help is greatly appreciated

Kinz
  • 310
  • 1
  • 2
  • 14
  • 1
    FYI, in Python you generally don't end statements with semicolons. – Taymon May 10 '12 at 06:12
  • Welcome to python - it's an amazing language! As a tip, you don't need to terminate statements with the ';' -- statements are ended by newlines, so just hit the return key! – parselmouth May 10 '12 at 06:12
  • possible duplicate of [UnboundLocalError in Python](http://stackoverflow.com/questions/9264763/unboundlocalerror-in-python) – Ferdinand Beyer May 10 '12 at 06:13
  • 1
    -1: On Stack Overflow, we expect users to show a little research effort before asking a new question. In your case, searching for the error you get (`UnboundLocalError`) should have let you to the solution in no time! – Ferdinand Beyer May 10 '12 at 06:14
  • I did. I didn't find anything. That would be why I came here. Obviously not everyone can find what they're looking for, when a post is nearly 3 months old. Sorry. – Kinz May 10 '12 at 06:16
  • You didn't find anything? What have you been searching for? You should really try to understand the exception traceback you get ("all gibberish"). Otherwise, you won't be able to solve *any* Python errors you get by your own. The exception message is "UnboundLocalError: local variable 'count' referenced before assignment". Pasting this in your favorite search engine would have given you the answer right away! – Ferdinand Beyer May 10 '12 at 06:20

3 Answers3

4

In Python, if you want to modify a global variable, you need to use the global keyword:

class inp(threading.Thread):
    def run(self):
        global count
        while count < 11:
            time.sleep(0.5)
            print("Thread 1!")
            count += 1

Otherwise, Python will treat count as a local variable and optimize access to it. This way, the local count is not defined yet in the while loop.

Also, get rid of the semicolons, they are not needed in Python!

Ferdinand Beyer
  • 64,979
  • 15
  • 154
  • 145
2

You have to declare that you intend to use the global count, not create a new local variable: Add global count to the run methods in both of your threads.

spinlok
  • 3,561
  • 18
  • 27
2

Since you are modifying the value of count, you'll need to declare it as global

class inp(threading.Thread):
    def run(self):
        global count
        while count < 11:
            time.sleep(0.5)
            print("Thread 1!")
            count += 1

class recv_oup(threading.Thread):
    def run(self):
        global count
        while count < 31:
            time.sleep(0.5)
            print("Thread 2!")
            count += 1
John La Rooy
  • 295,403
  • 53
  • 369
  • 502