Home > Enterprise >  How to let cmd-line continuous print log message?
How to let cmd-line continuous print log message?

Time:09-28

I wrote a simple app that I want to print logging message every seconds. I open a terminal and run it, it works well. But when I use mouse to click the terminal, it will not print logging message unless I print Enter in the terminal.

The Code

import logging
from PyQt5.QtCore import *

logging.basicConfig(level=logging.DEBUG)


app = QCoreApplication([])

timer = QTimer()
timer.setInterval(1000)
timer.timeout.connect(lambda: logging.info("abc"))
timer.start()

app.exec()

CodePudding user response:

Seems to be environment issue. Below example demonstrates that Qt logging works fine even if mouse button is pressed.

cat pyqt_logging_ex.py
#!/usr/bin/python3.9
import logging,os
from PyQt5.QtCore import *
from pynput import mouse,keyboard

def on_click(x, y, button, pressed):
    print("Mouse click")

def on_press(key):
    print("Key pressed, exiting")
    os._exit(0)

listener = keyboard.Listener(on_press=on_press)
listener.start()
listener = mouse.Listener(on_click=on_click)
listener.start()
print("Started listeners")

logging.basicConfig(level=logging.DEBUG)
app = QCoreApplication([])
timer = QTimer()
timer.setInterval(1000)
timer.timeout.connect(lambda: logging.info("abc"))
timer.start()
app.exec()

Run example:

pyqt_logging_ex.py
Started listeners
INFO:root:abc
INFO:root:abc
INFO:root:abc
Mouse click
Mouse click
INFO:root:abc
INFO:root:abc
Mouse click
Mouse click
INFO:root:abc
INFO:root:abc
qKey pressed, exiting

My environment:

uname -or ; lsb_release -d ; python3.9 --version
4.4.0-19041-Microsoft GNU/Linux
Description:    Ubuntu 20.04.3 LTS
Python 3.9.5
  • Related