The last time I used a row of functions, I used multiple threads.
# Coding = utf8''' def singles (): Sum = 0 for I in range (): t = reduce (lambda X, Y: x * y, range (1, I + 1) sum + = t return sum print singles () ''' # multi-threaded implementation, start five threads at once to calculate the factorial from 1 to 5, A global variable sum adds the result to import threading, time sum = 0 lock = threading. lock () def single (I): global sum, lock. acquire () t = reduce (lambda X, Y: x * y, range (1, I + 1) sum = sum + T print threading. currentthread (). getname (), t lock. release () def main (): global sum for I in range (1, 6): threading. thread (target = single, argS = (I,), name = 'thread' + STR (I )). start () time. sleep (1) print sum if _ name __= = '_ main _': Main ()
Without judging thread blocking, it is not safe to use sleep directly.