import concurrent.futures
import logging
import time
logging.basicConfig(level=logging.DEBUG, format='%(threadName)s: %(message)s')
def main():
with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:
f1 = executor.submit(worker, 2, 5)
f2 = executor.submit(worker, 2, 5)
logging.debug(f1.result())
logging.debug(f2.result())
def worker(x, y):
logging.debug('start')
time.sleep(3)
r = x * y
logging.debug(r)
logging.debug('end')
return r
if __name__ == '__main__':
main()
ThreadPoolExecutor-0_0: start
ThreadPoolExecutor-0_1: start
ThreadPoolExecutor-0_0: 10
ThreadPoolExecutor-0_1: 10
ThreadPoolExecutor-0_0: end
ThreadPoolExecutor-0_1: end
MainThread: 10
MainThread: 10
import concurrent.futures
import logging
import time
logging.basicConfig(level=logging.DEBUG, format='%(processName)s: %(message)s')
def worker(x, y):
logging.debug('start')
time.sleep(3)
r = x * y
logging.debug(r)
logging.debug('end')
return r
def main():
with concurrent.futures.ProcessPoolExecutor(max_workers=5) as executor:
f1 = executor.submit(worker, 2, 5)
f2 = executor.submit(worker, 2, 5)
logging.debug(f1.result())
logging.debug(f2.result())
if __name__ == '__main__':
main()
SpawnProcess-1: start
SpawnProcess-2: start
SpawnProcess-1: 10
SpawnProcess-2: 10
SpawnProcess-1: end
SpawnProcess-2: end
MainProcess: 10
MainProcess: 10