python basis of many threads of operation

# Multithreaded Example 

# 1 Example threaded
from Time Import SLEEP, the ctime
DEF Task1 (taskName):
for I in Range (2):
Print ( "% S S being executed%"% (taskName, the ctime ()))
SLEEP (2)

DEF Task2 (taskName):
for I in Range (2):
Print ( "% S S being executed%"% (taskName, the ctime ()))
SLEEP (. 1)

IF the __name__ == '__main__':
Task1 ( "tasks a")
Task2 ( "task II")


# Example 2
from Time Import SLEEP, the ctime
DEF task (taskName, Time):
for I in Range (2):
Print ( "executing% s% s"% (taskName , the ctime ()))
SLEEP (Time)

IF the __name__ == '__main__':
task ( "a task", 2)
task ( "task II",1)

if __name__ == '__main__':
Task ( "Task one", 2)
Task ( "Task II", 1)

# Example 3 to introduce threads
from Time Import SLEEP, the ctime
Import Threading # incorporated thread module
DEF Task (taskName, Time):
for I in Range (2) :
Print ( "% S S being executed%"% (taskName, the ctime ()))
SLEEP (Time)

# assembling all threads into the threads
threads = []

# memory task placement thread
t1 = threading.Thread (target = task, args = ( "a task", 2))
T2 = of the threading.Thread (target = task, args = ( "task II",. 1))
threads.append (T1)
threads.append (T2)

IF the __name__ = = '__main__':
# thread start start ()
for I in threads:
i.start ()
# end main thread
print ( "main thread end S%"% the ctime ())


#. 4.Multithreading - daemon threads the Join ()
from Time Import SLEEP, ctime
import threading # incorporated thread module
DEF Task (taskName, Time):
for I in Range (2):
Print ( "executing% S% S"% (taskName, the ctime ()))
SLEEP (Time)

# all threads assembly the threads of
threads = []
# memory task placement thread
t1 = threading.Thread (target = task, args = ( " a task", 2))
T2 = of the threading.Thread (target = task, args = ( "task two ", 1))
threads.append (T1)
threads.append (T2)

IF __name__ == '__main__':
# start thread start ()
for i in threads:
i.start ()
# daemon the Join ()
for i threads in:
i.join ()
# end of the main thread
print ( "end of the main thread S%"% ctime ())

# create a batch of Example 5 to generate optimized thread thread
from time import sleep, ctime
import threading # introduction thread module
DEF Task (taskName, Time):
for i in the Range (2):
Print ( "being executed% S% S"% (taskName, ctime ()))
SLEEP (Time)
# defined to store all tasks the list of
tasks = [( "task one", 2), ( "task II", 1)]
# all threads assembled into the threads of the
threads = []

# batch generation thread
DEF CreateThread ():
for taskName, Time in tasks :
T1 = of the threading.Thread (target = Task, args = (taskName, Time))
threads.append (T1)

IF the __name__ == '__main__':
# batch generation thread
CreateThread ()
# start thread start ()
for I in threads :
i.start ()
# daemon the Join ()
for i in threads:
i.join ()
# end the main thread
print ( "End main thread% S"% the ctime ())


# Example 6 according to the task and the time of the user input, batch production of threads
from Time Import SLEEP, the ctime
Import Threading # incorporated thread module
DEF Task (taskName, Time):
for I in the Range (2):
Print ( "being executed% S% S"% (taskName, ctime ()))
SLEEP (Time)

# define storage list of all tasks
# tasks = [( "job one", 2), ( "task II",. 1)]
tasks = []
# receiving task name and duration of user input, is added into the tasks
DEF rectask (taskName, time):
tasks.append ((taskName, time))

# all threads assembled into threads the
threads = []

# create a batch generation thread
DEF CreateThread ():
for taskName, Time in Tasks:
T1 = threading.Thread (target = Task, args = (taskName, Time))
threads.append (T1)

IF == __name__ '__main__':
nums = int (input ( 'Please enter the number of tasks you want to perform:'))
for i in the Range (nums): #range (2) 0, 1
taskName the INPUT = ( "Please enter the name of the task% s"% ( I +. 1))
time = int (iNPUT ( "enter the length% s of task execution"% (I +. 1)))
# tasks and duration assembled tasks list
rectask (taskName, time)
# batch generation thread
CreateThread ()
# start thread start ()
for i in threads:
i.start ()
# daemon the Join ()
for i in threads:
i.join ()
# end of the main thread
print ( "end of the main thread% s"% ctime ())

Guess you like

Origin www.cnblogs.com/Teachertao/p/11707845.html