0

I'm new to threading in python and I've began by creating a simple program which uses two threads to print the numbers 1-100. The first thread prints all of the odd numbers and the second thread prints all of the even numbers.

Initially, I encountered a problem where my threads were just printing the numbers as fast as they could, so the numbers weren't always in order. I don't know (and neither have I researched) how to synchronise threads in python so I've implemented a simple technique where I use Boolean flags and switch them on and off, and use a delay to 'sync' the threads.

Looking at my code, I've just realised that my flags have the exact same variable names as my threads.

Why isn't python throwing any errors?

My code:

import time, threading

def printOdd():

    global odd, even

    for i in range(1, 101, 2):

        while not odd:
            time.sleep(0.01)

        if odd:
            print(i)
            odd = False
            even = True

def printEven():

    global odd, even

    for i in range(2, 101, 2):

        while not even:
            time.sleep(0.01)


        if even:
            print(i)
            odd = True
            even = False


odd = True
even = False

odd = threading.Thread(target = printOdd)
even = threading.Thread(target = printEven)

odd.daemon = True
even.daemon = True

odd.start()
even.start()
4
  • Do not abuse your tag creation privileges. Commented Aug 5, 2018 at 10:44
  • @jpmc26 Sorry =) Commented Aug 5, 2018 at 10:46
  • Please can any (potential) downvoters explain why they're downvoting? Commented Aug 5, 2018 at 10:47
  • Watch this youtu.be/9zinZmE3Ogk And dig much further into concurrency. It's a topic that has an enormous amount of writing devoted to it. Commented Aug 5, 2018 at 10:47

1 Answer 1

2

What kind of errors do you expect?

  1. Python is dynamically typed, so you can assign the same variable to objects of different types no problem;
  2. That the even, odd variables are no longer assigned to the thread objects doesn't mean the threads don't run, so that's fine too;
  3. if condition: is actually if bool(condition):, and, provided that a Thread object is truthy, the first thread does get into the first if statement and assigns the even, odd variables to plain booleans.

It's simple to see that Thread objects are indeed truthy:

>>> import threading
>>> th = threading.Thread(target=print)
>>> bool(th)
True
Sign up to request clarification or add additional context in comments.

3 Comments

I wasn't expecting 1 to be a problem anyway. I understand your second point too, but surely the threads would throw an error when they ran because when the if conditions are evaluated inside the functions, odd and even are threads not Booleans?
Objects can be converted to booleans with bool(object), as described in the third point: bool(0) == bool(None) == False, bool("hello") == bool(1024) == True.
I knew you could evaluate strings and integers as booleans, but I didn't know that applied to all object types. Thanks, that makes sense!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.