Queue

정은경·2020년 5월 29일
0

1. 큐 구조

  • 줄을 서는 행위와 유사
  • FIFO (First In First Out)
  • LILO (Last In Last Out)

2. 파이썬 queue 라이브러리

  • 파이썬 queue 라이브러리의 종류: Queue(), LifoQueue(), PriorityQueue()
    • Queue(): 가장 일반적인 큐 자료 구조
    • LifoQueue(): 나중에 입력된 데이터가 먼저 출력되는 구조 (스택구조와 유사)
    • PriorityQueue(): 데이터마다 우선순위를 넣어서, 우선순위가 높은 순으로 데이터 출력
  • 프로그램을 작성할 때 프로그램에 따라 적합한 자료구조를 사용!

3. FIFO queue 사용하기

>>> import queue
>>>
>>> my_queue = queue.Queue()
>>>
>>> dir(my_queue)
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_get', '_init', '_put', '_qsize', 'all_tasks_done', 'empty', 'full', 'get', 'get_nowait', 'join', 'maxsize', 'mutex', 'not_empty', 'not_full', 'put', 'put_nowait', 'qsize', 'queue', 'task_done', 'unfinished_tasks']
>>>
>>> my_queue.qsize()
0
>>>
>>> my_queue.put('a')
>>> my_queue.put('b')
>>>
>>> my_queue.qsize()
2
>>>
>>> my_queue.get()
'a'
>>> my_queue.qsize()
1
>>>
>>> my_queue
<queue.Queue object at 0x000001CC0773B248>
>>> my_queue.queue()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'collections.deque' object is not callable
>>> my_queue.queue
deque(['b'])
>>>

4. LILO queue 구현하기

>>> import queue
>>>
>>> my_lifo_q = queue.LifoQueue()
>>>
>>> dir(my_lifo_q)
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_get', '_init', '_put', '_qsize', 'all_tasks_done', 'empty', 'full', 'get', 'get_nowait', 'join', 'maxsize', 'mutex', 'not_empty', 'not_full', 'put', 'put_nowait', 'qsize', 'queue', 'task_done', 'unfinished_tasks']
>>>
>>> my_lifo_q.put('a')
>>> my_lifo_q.qsize()
1
>>>
>>> my_lifo_q.put('b')
>>> my_lifo_q.qsize()
2
>>>
>>> my_lifo_q.get()
'b'
>>>
>>> my_lifo_q.queue()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'list' object is not callable
>>> my_lifo_q
<queue.LifoQueue object at 0x000001F9863EB248>
>>> my_lifo_q.queue
['a']
>>>

5. Priority queue 사용하기

>>> import queue
>>>
>>> my_pq = queue.PriorityQueue()
>>>
>>> dir(my_pq)
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_get', '_init', '_put', '_qsize', 'all_tasks_done', 'empty', 'full', 'get', 'get_nowait', 'join', 'maxsize', 'mutex', 'not_empty', 'not_full', 'put', 'put_nowait', 'qsize', 'queue', 'task_done', 'unfinished_tasks']
>>>
>>> my_pq.qsize()
0
>>>
>>> my_pq.put((10, 'a'))
>>> my_pq.put((5, 'b'))
>>> my_pq.put((15, 'c'))
>>>
>>> my_pq.qsize()
3
>>>
>>> my_pq.get()
(5, 'b')
>>>
>>> my_pq.put((15, 'd'))
>>> my_pq.put((10, 'e'))
>>>
>>> my_pq.get()
(10, 'a')
>>>
>>> print(my_pq.queue)
[(10, 'e'), (15, 'c'), (15, 'd')]
>>>
>>>

6. Queue는 어디에 쓰이니?

  • 멀티 태스킹을 위한 프로세스 스케줄링방식을 구현하기 위해 많이 사용됨

7. enqueue, dequeue 구현해보자

class MyQueue(object):
    def __init__(self):
        self._queue_list = list()

    def enqueue(self, data):
        self._queue_list.append(data)

    def dequeue(self):
        returned_data = self._queue_list[0]
        del self._queue_list[0]
        return returned_data


mq = MyQueue()
mq.enqueue(1)
mq.enqueue(2)
mq.enqueue(3)

print(mq.dequeue())
print(mq.dequeue())
print(mq.dequeue())
"""
1
2
3
"""

Reference

profile
#의식의흐름 #순간순간 #생각의스냅샷

0개의 댓글