简体   繁体   中英

Function "break" after certain time

Lets say certain time its 5 hours.

Call this function, inside 2 different threads for a long time can "break"?

Example:

from threading import Thread

function( ):
    do something can take one second to finish.

threadOne( ):
    while True:
        function( )

threadTwo( ):
    while True:
        function( )

Thread( target = threadOne ).start( )
Thread( target = threadTwo ).start( )

The 2 threads never ends.

You have infinite loops. Based on what you have, function() would have to throw an exception to do what you want. I would recommended not doing that and changing your code so as to remove the infinite loop. Perhaps looping conditionally based on the return of function().

Your algorithm is incomplete and a bit lacking, in both of your threads you state that while either of them are true, do the function.

Well, how do they end? When do they become false? Throw in something that you want to do that causes thread one to end, because if thread 1 does not break but function 1 breaks then it will just call the function AGAIN and keep looping until you break the program.

The functions can break on their own, but you NEED to do something to make the threads terminate.

For example, if I created a for loop inside of your thread while it was true, and put both functions inside that for loop and then made the for loop run through until an integer reached 9999 the function should just end and the thread should end, right? Nope.

it calls the for loop again and keeps making your poor computer count 9999 until you make it end.

Always create an end protocol.

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