1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

並列化

Last updated at Posted at 2020-04-06
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() #worker1の処理が終わるまで待つ。これがないと先のt2が終了した時点でプログラムが終了してしまう
    t2.join() #デーモン化していないので書かなくてもいい
出力
rename worker: start
Thread-1: start
Thread-1: 100
Thread-1: 200
started
Thread-1: end
rename worker: end
1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?