9

I am new to python I have very little knowledge about threads in python. Here is my sample code.

import threading
from threading import Thread
import time

check = False

def func1():
    print ("funn1 started")
    while check:
        print ("got permission")

def func2():
    global check
    print ("func2 started")
    time.sleep(2)
    check = True
    time.sleep(2)
    check = False

if __name__ == '__main__':
    Thread(target = func1).start()
    Thread(target = func2).start()

What I want is to see see "got permission" as the output. But with my current code it is not happening. I assume that the func1 thread is closed before func2 changes the check value to True.

How can I keep func1 alive? I have researched on the internet but I could not found a solution. Any help would be appreciated. Thank you in advance!

3 Answers 3

5

The problem here is that func1 performs the check in the while loop, finds it is false, and terminates. So the first thread finishes without printing "got permission".

I don't think this mechanism is quite what you are looking for. I would opt to use a Condition like this,

import threading
from threading import Thread
import time

check = threading.Condition()

def func1():
    print ("funn1 started")
    check.acquire()
    check.wait()
    print ("got permission")
    print ("funn1 finished")


def func2():
    print ("func2 started")
    check.acquire()
    time.sleep(2)
    check.notify()
    check.release()
    time.sleep(2)
    print ("func2 finished")

if __name__ == '__main__':
    Thread(target = func1).start()
    Thread(target = func2).start()

Here the condition variable is using a mutex internally to communicate between the threads; So only one thread can acquire the condition variable at a time. The first function acquires the condition variable and then releases it but registers that it is going to wait until it receives a notification via the condition variable. The second thread can then acquire the condition variable and, when it has done what it needs to do, it notifies the waiting thread that it can continue.

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

4 Comments

Thanks. can you explain it a bit
Sure! Edited the answer to provide explanation.
But actually func1 should not finished until func2 finished. That mean func2 keep changing variable check until finished
Ok, I wasn't really sure about the semantics that you were going for in your example. It sounds like you have two conditions that you are interested in: the completion of the 2nd thread, and the change in the state of 'check'. Consider using a threading.Event to communicate the completion of the 2nd thread.
0
from threading import Thread
import time

check = False

def func1():
    print ("funn1 started")
    while True:
        if check:
            print ("got permission")
            break

def func2():
    global check
    print ("func2 started")
    time.sleep(2)
    check = True
    time.sleep(2)
    check = False

if __name__ == '__main__':
    Thread(target = func1).start()
    Thread(target = func2).start()

Comments

0

func1 must be like this

def func1():
    print("func1 started")
    while True:
        if check:
            print("got permission")
            break
        else:
            time.sleep(0.1)

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.