Referring to the thread module, we have to solve the thread lock ourselves. Actually, there's nothing. Just now people are more lazy, since there is a high-level package of functions why to write their own.
So there is the threading.
It's all the same, actually.
One of the simplest threading codes:
1 ImportThreading#Import thrteading Module2 3 defRun ():#thread function, print thread name and I value4 forIinchRange (100):5 Print(Threading.current_thread (). GetName () +"-------"+str (i))6 7Threads = []#Create a list of threads8 9 forIinchRange (10):#Create a thread and add a list of threads into the threadTent = Threading. Thread (target =run) One A threads.append (t) - - forIinchThreads#Start All Threads the I.start () - - forIinchThreads#Main Line waits for all child threads to end -I.join ()
The above code differs from the thread module only in that the join () function replaces us to write the thread lock.
That's all.
If it can be simpler:
1 Import Threading 2 3 def run (): 4 for in range:5 print(i)67 t = Threading. Thread (target = run)8t.start ()9 t.join ()
The main function does nothing but start a sub-thread.
Python multithreaded threading