LoginSignup
0
0

More than 3 years have passed since last update.

threading.enumerate

Last updated at Posted at 2020-04-06

threading.enumerateで生存中のThreadオブジェクト全てのリストを取得できます。

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')

if __name__ == '__main__':
    #threads = []
    for _ in range(5):
        t = threading.Thread(target=worker1)
        t.setDaemon(True)
        t.start()
        #threads.append(t)

    for thread in threading.enumerate():
        if thread is threading.currentThread():
            print(thread)
            continue
        thread.join()
Thread-1: start
Thread-2: start
Thread-3: start
Thread-4: start
Thread-5: start
<_MainThread(MainThread, started 4599717312)>
Thread-1: end
Thread-2: end
Thread-5: end
Thread-4: end
Thread-3: end
0
0
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
0
0