thread outputs may be intermixed in this version arbitrarily ""import _thread as threadtime def counter(myidcount)for in range(count)time sleep( print('[% =% (myidi)function run in threads for in range( )thread start_new_thread(counter( )spawn threads each thread loops times time sleep( print('main thread exiting 'don' exit too early simulate real work each parallel copy of the counter function simply counts from zero up to four here and prints message to standard output for each count notice how this script sleeps for seconds at the end on windows and linux machines this has been tested onthe main thread shouldn' exit while any spawned threads are running if it cares about their workif it does exitall spawned threads are immediately terminated this differs from processeswhere spawned children live on when parents exit without the sleep herethe spawned threads would die almost immediately after they are started this may seem ad hocbut it isn' required on all platformsand programs are usually structured such that the main thread naturally lives as long as the threads it starts for instancea user interface may start an ftp download running in threadbut the download lives much shorter life than the user interface itself later in this sectionwe'll also see different ways to avoid this sleep using global locks and flags that let threads signal their completion moreoverwe'll later find that the threading module both provides join method that lets us wait for spawned threads to finish explicitlyand refuses to allow program to exit at all if any of its normal threads are still running (which may be useful in this casebut can require extra work to shut down in othersthe multiprocessing module we'll meet later in this also allows spawned children to outlive their parentsthough this is largely an artifact of its process-based model nowwhen example - is run on windows under python here is the output getc:\pp \system\threadspython thread-count py [ = [ = [ = [ = [ = [ = [ = parallel system tools