#8 "ValueError: can't have unbuffered text I/O" in Python 3.6
Closed: Invalid 2 years ago by bignose. Opened 5 years ago by leafonsword.

I use following code runing in Pyhton 3.6:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/usr/bin/env python3
import logging
import time

# third party libs
from daemon import runner


class Updater():
    def __init__(self):
        self.stdin_path = '/dev/null'
        self.stdout_path = '/dev/tty'
        self.stderr_path = '/dev/tty'
        self.pidfile_path = '/tmp/testdaemon.pid'
        self.pidfile_timeout = 5

    def run(self):
        while True:
            # Main code goes here ...
            # Note that logger level needs to be set to logging.DEBUG before this shows up in the logs
            logger.debug("Debug message")
            logger.info("Info message")
            logger.warn("Warning message")
            logger.error("Error message")
            time.sleep(10)


app = Updater()
logger = logging.getLogger("DaemonLog")
logger.setLevel(logging.INFO)
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
handler = logging.FileHandler("/tmp/testdaemon.log")
handler.setFormatter(formatter)
logger.addHandler(handler)

daemon_runner = runner.DaemonRunner(app)

# This ensures that the logger file handle does not get closed during daemonization
daemon_runner.daemon_context.files_preserve = [handler.stream]
daemon_runner.do_action()

but it gives follow error:

gf@guofengdeMacBook-Pro /p/tmp> ./test.py start
Traceback (most recent call last):
  File "./test.py", line 36, in <module>
    daemon_runner = runner.DaemonRunner(app)
  File "/usr/local/miniconda3/lib/python3.6/site-packages/daemon/runner.py", line 120, in __init__
    app.stderr_path, 'w+t', buffering=0)
ValueError: can't have unbuffered text I/O

This behaviour is a little different on Python 3.7:

$ python3 issue-8-test.py start
Traceback (most recent call last):
  File "issue-8-test.py", line 36, in <module>
    daemon_runner = runner.DaemonRunner(app)
  File "[…]/daemon/runner.py", line 115, in __init__
    self.daemon_context.stdout = open(app.stdout_path, 'w+t')
io.UnsupportedOperation: File or stream is not seekable.

This is a more descriptive message for essentially the same bug.

The issue is with numerous changes to the stream types in Python 2 and Python 3. Text mode and buffering both behave differently from when this example runner module was first written.

The runner example module is not formally part of the daemon library. It is deprecated and will eventually be removed.

Thanks for the report.

Metadata Update from @bignose:
- Issue close_status updated to: Invalid
- Issue status updated to: Closed (was: Open)

2 years ago

Login to comment on this ticket.

Metadata