The story that `while queue` did not work in python

Queue in python

There are multiple queue implementations, but the difference between queue.Queue and collections.deque became a problem. Originally written using deque, rewritten using Queue

Original code


from collections import deque

queue = deque()
queue.append("hogehoge")
while queue:
    item = queue.popleft()
    if n := len(item) == 1:
        print(item)
    else:
        queue.append(item[:n//2])
        queue.append(item[n//2:])

Rewritten code


from queue import Queue

queue = Queue()
queue.put("hogehoge")
while queue:
    item = queue.get()
    if n := len(item) == 1:
        print(item)
    else:
        queue.put(item[:n//2])
        queue.put(item[n//2:])

Executing the code below in this state resulted in an infinite loop.

Queue is judged to be True even if it is empty

Although there is no direct description in https://docs.python.org/ja/3/library/queue.html, it was output as such in REPL.

REPL


from queue import Queue
q = Queue()
bool(q)
> True

q.put("hoge")
bool(q)
> True

Apparently there is Queue.empty (), so I tried to rewrite it using it, but the infinite loop was not resolved.

Queue.empty()Code rewritten using


from queue import Queue

queue = Queue()
queue.put("hogehoge")
while not queue.empty():
    item = queue.get()
    if n := len(item) == 1:
        print(item)
    else:
        queue.put(item[:n//2])
        queue.put(item[n//2:])

Recommended Posts

The story that `while queue` did not work in python
The story that 2D list replacement did not work in python
The story that yapf did not work in vscode
The story that sendmail that can be executed in the terminal did not work with cron
I wrote the queue in Python
[Python] Tensorflow 2.0 did not support Python 3.8, so the story of downgrading Python
The story of debugging in the local environment because the compilation did not work with Read the Docs
The story of the release work of the application that Google does not tell
The story of exclude / include that Serverless Framework beginners misunderstood (did not understand) in beginner Pythonista
A story that didn't work when I tried to log in with the Python requests module
Results that did not get caught in the search with this word
About the case that the speaker did not make sound in Ubuntu LTS 20.04
The story of reading HSPICE data in Python
The one that displays the progress bar in Python
The story that fits in with pip installation
A story that did not end with exit when turning while with pipe input
Examples and solutions that the Python version specified in pyenv does not run
Queue processing in Python
Download the file while viewing the progress in Python 3.x
Key input that does not wait for key input in Python
Find the part that is 575 from Wikipedia in Python
Not being aware of the contents of the data in python
Modules that may go through the shell in Python
There is a pattern that the program did not stop when using Python threading
Test.py is not reflected on the web server in Python3.
Download the file in Python
Find the difference in Python
When writing to a csv file with python, a story that I made a mistake and did not meet the delivery date
Get the value while specifying the default value from dict in Python
Stack and Queue in Python
The story that Python stopped working with VS Code (Windows 10)
A story that pyenv is stuck because the python execution command PATH does not pass
A story that I did not know how to load a mixin when making a front with the django app [Beginners learn python with a reference book in one hand]
If you think that the person you put in with pip doesn't work → Maybe you are using python3?
The story of creating a bot that displays active members in a specific channel of slack with python
Play a sound in Python assuming that the keyboard is a piano keyboard
Movement that changes direction in the coordinate system I tried Python 3
A story that struggled to handle the Python package of PocketSphinx
New Python grammar and features not mentioned in the introductory book
A record that GAMEBOY could not be done in Python. (PYBOY)
The story that Pathlib could not access the folder containing the half-width space
How to judge that the cross key is input in Python3
A function that measures the processing time of a method in python
The problem that the version of Vue CLI did not go up
Review the basics in 1 minute! Python Priority queue for fast minimums
The story that inheritance behavior is quite different in each language
[Python] Do not put Japanese in the path used by OpenCV
The story of making a module that skips mail with python
Arrow keys do not work in zsh + python shell on mac
The story of Python and the story of NaN
Getting the arXiv API in Python
Python in the browser: Brython's recommendation
The story of participating in AtCoder
Save the binary file in Python
Hit the Sesami API in Python
Get the desktop path in Python
Get the script path in Python
In the python command python points to python3.8
Implement the Singleton pattern in Python
The story of the "hole" in the file
Hit the web API in Python