[PYTHON] Parallelization

import logging
import threading
import time

logging.basicConfig(level=logging.DEBUG, format='%(threadName)s: %(message)s')


def worker1():
    logging.debug('start')
    time.sleep(5)
    logging.debug('end')


def worker2(x, y=1):
    logging.debug('start')
    logging.debug(x)
    logging.debug(y)
    time.sleep(2)
    logging.debug('end')


if __name__ == '__main__':
    t1 = threading.Thread(name='rename worker', target=worker1)
    t1.setDaemon(True) 
    t2 = threading.Thread(target=worker2, args=(100,), kwargs={'y': 200})
    t1.start()
    t2.start()
    print('started')
    t1.join() #Wait until worker1 finishes processing. Without this, the program will end when the previous t2 ends.
    t2.join() #You don't have to write it because it is not daemonized.

output


rename worker: start
Thread-1: start
Thread-1: 100
Thread-1: 200
started
Thread-1: end
rename worker: end

Recommended Posts

Parallelization
parallelization of class method