1

I have a script written in python 2.7 that calls for a thread. But, whatever I do, the thread won't call the function.

The function it calls:

def siren_loop():
    while running:
        print 'dit is een print'

The way I tried to call it:

running = True
t = threading.Thread(target=siren_loop)
t.start()

or:

running = True
thread.start_new_thread( siren_loop, () )

I even tried to add arguments to siren_loop to see if that would work, but no change. I just can't get it to print the lines in the siren_loop function.

I also tried many other strange things, which obviously didn't work. What am I doing wrong?

edit: Since people said it worked, I tried to call the thread from another function. So it looked something like this:

def start_sirene():
    running = True
    t = threading.Thread(target=siren_loop)
    t.start()

And then that part was called from:

if zwaailichtbool == False:
        start_sirene()
        print 'zwaailicht aan'
        zwaailichtbool = True
        sleep(0.5)

Maybe that could cause the problem? The print statement in the last one works, and when I added a print before or after the thread statement it also worked.

9
  • I just tried you code, using the first way you tried to call it, and it printed out your statement in siren_loop continuously. So I'm not sure what the problem is. Commented Feb 2, 2017 at 19:30
  • Also verified using QPython. Your code is fine. Commented Feb 2, 2017 at 19:32
  • I edited the question to put a bit of the rest of my code there. Commented Feb 2, 2017 at 19:36
  • Perhaps zwaailichtbool is not False? Commented Feb 2, 2017 at 19:38
  • 1
    What does the rest of the program do after creating the thread? If it exists immediately, the thread won't have had a chance to run. Also, how are you running all this? Are you sure you have a console somewhere where the output of the print statements should go to? Finally, are you sure you're running this with Python 2.7? Because when you're running it with Python 3.x the thread will do nothing but crash with a syntax error. Commented Feb 2, 2017 at 20:08

3 Answers 3

1

So, after trying various things for hours i and hours, I found a solution but still don't understand the problem.

Apparently the program didnt like the many steps. I took one step away (the start siren method) but used the exact same code, and suddenly it worked. Stl no clue why that was the problem. If anybody knows, please enlighten me xD

Sign up to request clarification or add additional context in comments.

Comments

0

running is a local variable in your code. Add global running to start_sirene()

4 Comments

This what I thought, but wouldn't this have given the OP an "undefined" error?
You probably initialized the global running somewhere else.
Thats not the problem. Running is initialized at the beginning of my code, and it works well.
Unless you add global x to a function in Python, x = something will always make it a local variable for that function, even if you have another x somewhere else in your code. Try it :-)
0

It's working perfectly fine for me, you can also specify running as keyword arguments to the thread_function.

import threading

def siren_loop(running):
    while running:
        print 'dit is een print'

t = threading.Thread(target=siren_loop, kwargs=dict(running=True))
t.start()

Comments

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.