简体   繁体   中英

Why does time.sleep(0) lets another thread to do stuff when removing time.sleep continues to run until loop is finished?

i am learning about threads and got one thing confusing.

from threading import Thread
from time import sleep
global a
a=0
def th1():
    lasta=0
    while(a<200):
        if(a!=lasta):
            lasta=a
            print(a) 
thrd=Thread(target=th1)
print(a)
thrd.start()
for i in range (1,200):
    a+=1
    sleep(0)

this prints numbers from 0 to 199, but

from threading import Thread
from time import sleep
global a
a=0
def th1():
    lasta=0
    while(a<200):
        if(a!=lasta):
            lasta=a
            print(a) 
thrd=Thread(target=th1)
print(a)
thrd.start()
for i in range (1,200):
    a+=1

this code only prints 0 and 199.

I think what's happening is that in second code there's not the (lats say) stop statement that would make program execute different portion of the code while first one stops the loop and gives another thread the possibility to execute. then it checks if 0 seconds went and continues from for loop. i dont know if im right, please if you could help me explain what is really going on i would be glad. also how can i master this kind of things? for example to run two threads continuously and let them do stuff according to one global variable. because as i clearly see even though i'm using different thread, they aren't really doing stuff together, they are still waiting for each other

Thanks!

Your specific question is quite correct: a 0-second time-out is quite different from no statement at all. As you guess, this suspends the running thread and allows another thread to receive control of the CPU.

If you use merely multi-threading, then you get the interleaving situation you describe here: you have two logical execution threads, but only one logical processor . For parallel processing, you need multi-processing . There are many tutorials and examples available on line; simply search for "Python multiprocessing tutorial".

Your request "how can i master this kind of things?" is far too general to be a Stack Overflow question. You master this knowledge as you master any knowledge: you find materials that match your learning styles, work through those materials, and practice.

How multiple threads will run depends on the environment. Threads do not 'do stuff together'. They run somewhat independently.

On a single processor machine, or in a program which is running multiple threads on a single processor, they never run at the same time. Each thread gets a time slice. It runs for a fixed amount of time (unless it yields early, as with sleep). When its time slice is finished, the next thread runs for a fixed amount of time, or until it yields.

In your first example, the main thread yields each time it increments, so the th1 thread will run once per increment, and will see each number.

In the second example, the main thread will run a full timeslice before the th1 thread is given execution time. This is sufficient for the loop in the main thread to cycle many times. When the th1 thread runs again, it has 'missed' many values of a .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM