0
4

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-19
import logging
import queue
import threading
import time

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


def worker1(queue):
    logging.debug('start')
    queue.put(100) #[100, 200]
    time.sleep(5)
    queue.put(200)
    logging.debug('end')


def worker2(queue):
    logging.debug('start')
    logging.debug(queue.get())
    logging.debug(queue.get()) #キューに値(200)が入るまで待機してくれます
    logging.debug('end')


if __name__ == '__main__':
    queue = queue.Queue()

    t1 = threading.Thread(target=worker1, args=(queue,))
    t2 = threading.Thread(target=worker2, args=(queue,))
    t1.start()
    t2.start()
Thread-1: start
Thread-2: start
Thread-2: 100
Thread-1: end
Thread-2: 200
Thread-2: end
0
4
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
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?