daemon.DaemonContext

R

Rebelo

i get : IOError: [Errno 9] Bad file descriptor
when i have logging and using daemon.DaemonContext()
i tried passing :
fh = logging.handlers.TimedRotatingFileHandler(LOG_FILENAME, 'midnight',
encoding='utf-8')
with :
context = daemon.DaemonContext()
context.files_preserve=[fh]

but no good
what am i doing wrong?
 
R

Rebelo

when i use this :
context = daemon.DaemonContext(stdin=sys.stdin, stdout=sys.stdout,
files_preserve=[fh], signal_map = {signal.SIGTERM:
'terminate',signal.SIGHUP: 'terminate'})

i don't get error but i still can't write to log file
what am i doing wrong?
 
V

Vinay Sajip

when i use this :
 context = daemon.DaemonContext(stdin=sys.stdin, stdout=sys.stdout,
files_preserve=[fh], signal_map = {signal.SIGTERM:
'terminate',signal.SIGHUP: 'terminate'})

i don't get error but i still can't write to log file
what am i doing wrong?

Not sure, offhand.

Change to a script which just writes to the file (rather than going
via logging) - does that work? What platform are you on, what version
etc?

It's unlikely to be a logging-related problem: more likely it's to do
with file descriptors.

Regards,

Vinay Sajip
 
V

Vinay Sajip

when i use this :
 context = daemon.DaemonContext(stdin=sys.stdin, stdout=sys.stdout,
files_preserve=[fh], signal_map = {signal.SIGTERM:
'terminate',signal.SIGHUP: 'terminate'})

i don't get error but i still can't write to log file
what am i doing wrong?

My guess is - files_preserve needs to be passed a file handle and not
a logging handler.

Regards,

Vinay Sajip
 
R

Rebelo

Vinay said:
when i use this :
context = daemon.DaemonContext(stdin=sys.stdin, stdout=sys.stdout,
files_preserve=[fh], signal_map = {signal.SIGTERM:
'terminate',signal.SIGHUP: 'terminate'})

i don't get error but i still can't write to log file
what am i doing wrong?

My guess is - files_preserve needs to be passed a file handle and not
a logging handler.

Regards,

Vinay Sajip

thnx for help.
writing to a file works, but i need logging.
do you know where can i find good documnetation for python-daemon?
 
R

Rebelo

i found a crude workaround:
i wrote a function in which i start logging after deamon starts
 
V

Vinay Sajip

Vinay said:
when i use this :
 context = daemon.DaemonContext(stdin=sys.stdin, stdout=sys.stdout,
files_preserve=[fh], signal_map = {signal.SIGTERM:
'terminate',signal.SIGHUP: 'terminate'})
i don't get error but i still can't write to log file
what am i doing wrong?
My guess is - files_preserve needs to be passed a file handle and not
alogginghandler.

Vinay Sajip

thnx for help.
writing to a file works, but i needlogging.
do you know where can i find good documnetation for python-daemon?

No,

but see this post:

http://groups.google.com/group/comp.lang.python/msg/851ce78e53812ade

It may lead you to more information. The thread shows that Sean
DiZazzo got logging working with the package.

I think you just have to pass the file object used by the handler
(fh.stream) in the files_preserve array.

Regards,

Vinay Sajip
 
R

Rebelo

Vinay said:
Vinay said:
when i use this :
context = daemon.DaemonContext(stdin=sys.stdin, stdout=sys.stdout,
files_preserve=[fh], signal_map = {signal.SIGTERM:
'terminate',signal.SIGHUP: 'terminate'})
i don't get error but i still can't write to log file
what am i doing wrong?
My guess is - files_preserve needs to be passed a file handle and not
alogginghandler.
Regards,
Vinay Sajip
thnx for help.
writing to a file works, but i needlogging.
do you know where can i find good documnetation for python-daemon?

No,

but see this post:

http://groups.google.com/group/comp.lang.python/msg/851ce78e53812ade

It may lead you to more information. The thread shows that Sean
DiZazzo got logging working with the package.

I think you just have to pass the file object used by the handler
(fh.stream) in the files_preserve array.

Regards,

Vinay Sajip
thank you.
 
R

Rebelo

Ben said:
This is correct. As the ‘DaemonContext.__init__’ docstring says::

| `files_preserve`
| :Default: ``None``
|
| List of files that should *not* be closed when starting the
| daemon. If ``None``, all open file descriptors will be closed.
|
| Elements of the list are file descriptors (as returned by a file
| object's `fileno()` method) or Python `file` objects. Each
| specifies a file that is not to be closed during daemon start.

The docstrings and PEP 3143 are currently the best documentation for
‘python-daemon’; both describe the ‘files_preserve’ option as above.

As expressed in the referenced message, I would welcome someone writing
better documentation and contributing patches.
It may lead you to more information. The thread shows that Sean
DiZazzo got logging working with the package.

It's important to note (as you did, thanks Vinay) that none of the
‘logging.handlers’ are file descriptors. Nor are they file objects.

If we can get a Python ‘file’ object, we can use its ‘fileno()’ method
to get the file descriptor.

So how do we get the file object for a logging handler? The ‘logging’
module documentation doesn't mention any way to get at the stream object
for the handlers.

But looking at the source code for ‘StreamHandler’ on my system,
‘/usr/lib/python2.5/logging/__init__.py’, shows a ‘stream’ attribute
that is bound to the stream object. It's not marked private (i.e. it's
not named with a leading underscore), so one presumes it is part of the
public API.
I think you just have to pass the file object used by the handler
(fh.stream) in the files_preserve array.

Not quite. As the docs specify, you need to pass the *file descriptors*
for the files you want preserved. For regular Python file objects, the
‘file.fileno()’ method returns the file descriptor:

lh = logging.handlers.TimedRotatingFileHandler(
LOG_FILENAME,
# …
)
log_stream_descriptor = lh.stream.fileno()

daemon_context.files_preserve = [log_stream_descriptor]


thank you both for a detailed explanation.
 
V

Vinay Sajip

Not quite. As the docs specify, you need to pass the *file descriptors*
for the files you want preserved.

Okay, but the docstring you quoted:

"Elements of the list are file descriptors (as returned by a file
object's `fileno()` method) or Python `file` objects."

implies that fh.stream would work as well as fh.stream.fileno(), and I
presume you internally do a hasattr(thingy, 'fileno') to get the
descriptor.

Regards,

Vinay Sajip
 
W

wonko

I know it's 4 years later, but I'm currently battling this myself. I do exactly this and yet it doesn't appear to be keeping the filehandler open. Nothing ever gets written to logs after I daemonize!
 
E

Ethan Furman

I know it's 4 years later, but I'm currently battling this myself. I do exactly this and yet it doesn't appear to be keeping the filehandler open. Nothing ever gets written to logs after I daemonize!

You didn't include any context (important after four years!) so what are you talking about? And did you target the
correct list?
 
W

wonko

I know it's 4 years later, but I'm currently battling this myself. I do exactly this and yet it doesn't appear to be keeping the filehandler open. Nothing ever gets written to logs after I daemonize!

Ok, made it work, although I think this goes against the documentation as well as what's here.

I changed:

context = daemon.DaemonContext(
# Stuff here
)
context.files_preserve[fh.stream]

to:

context = daemon.DaemonContext(
# Stuff here
files_preserve[fh.stream]
)

And now it works.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top