While debugging a queue example below, I found a weird (to me) behavior related to the print function in Python 3.6. The below code works as expected however, if I change the "logger.info" with "print" function in the consumer function below, the consumer function hangs. Why is that so?
from queue import Queue
from threading import Thread
import logging
logging.basicConfig(format='%(asctime)s; %(name)s; %(levelname)s; %(message)s', level=logging.DEBUG)
logger = logging.getLogger(__name__)
logger.info('Starting...')
def consumer(q):
while True:
item = q.get()
logger.info ("Working on item: {}".format(item))
def main():
q = Queue()
t = Thread(target=consumer, args=(q,))
t.start()
for i in range(3):
q.put(i)
if __name__ == '__main__':
main()
Output 1:
2018-02-20 09:24:29,630; __main__; INFO; Starting...
2018-02-20 09:24:29,631; __main__; INFO; Working on item: 0
2018-02-20 09:24:29,631; __main__; INFO; Working on item: 1
2018-02-20 09:24:29,631; __main__; INFO; Working on item: 2
Output 2:
2018-02-20 09:26:14,497; __main__; INFO; Starting...