PythonStudy-- single-threaded concurrent realization

# Using a plurality of tasks concurrently yield achieved threaded 
# primer: Builder Review 
# DEF func1 (): 
#      Print ( '. 1') 
#      yield 
#      Print ( '2') 
#      yield 
#      Print ( '. 3') 
#      yield 


# = func1 RES () 
# Print (RES) # <Object Generator func1 AT 0x109032e60> 
# Next (RES). 1 # 
# Next (RES) # 2 
# Next (RES). 3 # 

# ---------- -------------------------------------------- 
# official for single use yield thread concurrency multiple tasks 
# Import Time 
# 
# that # summing function is a function concurrent
# def func1():
#     a = 1
#     for i in range(1000):
#         a += 1
#         print("a run")
#         yield
#
# def func2():
#     res = func1()
#     b = 1
#     for i in range(1000):
#         b += 1
#         print("b run")
#         next(res)
#
# start_time = time.time()
# func2()
# end_time = time.time()
# print("耗时>>>%s"%(end_time-start_time))
#>>> .007849931716918945 
# --------------------------------------------- ---------- 

# refrain yield concurrent program (default operation) 
# Import Time 
# 
# 
# DEF func1 (): 
#      A. 1 = 
#      for I in Range (1000): 
#          A. 1 = + 
#          Print ( "A RUN") 
# 
# 
# DEF func2 (): 
#      B =. 1 
#      for I in Range (1000): 
#          B +. 1 = 
#          Print ( "RUN B") 
# 
# 
# START_TIME the time.time = ( ) 
# func1 () 
#func2 () 
# END_TIME the time.time = () 
# Print ( "% S Processed >>>"% (END_TIME-START_TIME)) 
# # Processed >>> .006770133972167969 

# we have found that by comparing the numerical 
# using the yield force generator instead of a single thread concurrency function leads to increased time to slow down with

 

Guess you like

Origin www.cnblogs.com/tingguoguoyo/p/10990774.html