Closed hardbyte closed 8 years ago
Original comment by Brian Thorne (Bitbucket: hardbyte, GitHub: hardbyte):
Thanks for the bug report Mathias, sorry it has taken me a while to look at it.
A context manager would indeed be a much nicer api for the Listeners.
In this case I wonder if we gain anything having the Listener's extra thread. Perhaps we can just connect the Printer
to the Bus
. E.g:
with can.Printer(results.log_file) as printer:
for msg in bus:
printer.on_message_received(msg)
The problem in your solution appears to be that after __exit__
but before the whole program has stopped a message is received by the printer
.
I belive the fix being that __exit__
should set something in the Printer
that disables handling messages in the on_message_received
method. For instance a finished
boolean. If you have this all going would you mind contributing it back? Feel free to be more ambitious with changes if you wish!
Originally reported by: Mathias Giacomuzzi (Bitbucket: mgiaco, GitHub: mgiaco)
So I think there is something wrong with the current implementation of the can_logger. So tried to start a can logger with
So here I get nothing.
Here I get my messages.
So I dive into the implementation and there I found out that the file write has no flush. And what I read in the docs someone must close the file, otherwise it will not write it if no flush is used. So I see the Printer hast a del (destructor) which should close the file. But this destructor will never be called in this implementation. See also here: http://stackoverflow.com/questions/865115/how-do-i-correctly-clean-up-a-python-object
So dose this really work for other?
Is there a way to change the Listeners to the with statement usage? So I tried that but then I have some problem with the rx thread.
And in CAN.py I add:
It works now but I get an exception on close.
mathias