vorner / pyo3-log

Logging bridge from pyo3 native extension to python

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Logging from pure Rust

danielvschoor opened this issue · comments

Is it possible to log from pure Rust(not wrapped with pyfunction or pymethods)?

Background:
I'm starting a Rust websocket server from Python, which then runs in its own thread. Logging does not work as soon as I move into pure Rust code

I know there's a slim chance, but I figured I'd ask anyway

I'm not sure I understand your problem. In theory, if you do set up the logging through this crate, the global logger should be available and working well. Even if you start a separate thread from one of these wrapped methods, it should still ‒ in theory ‒ work.

So, if you can better explain what doesn't work, we can try figuring out why, if it's a bug in the library, or what exactly happens. A reproducer (code that doesn't do what it should) would probably be best.

I'll create a minimal example in a few hours.

Here is a minimal (as far as possible) example:
https://github.com/danielvschoor/pyo3-log-minimal-example

maturin develop -> python test.py

The run method on WrappedStruct does not log at all, which is consistent with what I've seen in my own library as well.

The issue could very well be on my side.

I haven't been able to run it yet (for some arcane reasons, my virtualenv stopped working, I'll have to solve that first). But reading the code, this indeed sounds like a bug somewhere. I'm not sure where exactly (I don't think pyo3-log itself does anything fishy with threads, so maybe it's something pyo3 should or should not be doing), this needs some further investigation.

But the main message is, this is intended to work.

I've been playing around with the code to see if I can figure out the issue, and I found it that if I wrap self.wrapped_struct.run().join() in py.allow_threads(), logging works as expected.
i.e:

py.allow_threads(move||{self.wrapped_struct.run().join();});

instead of just

self.wrapped_struct.run().join();

Thanks for your help!

That's an interesting find, I see what's been happening now (you're waiting for the thread, not running it in background, so the GIL is still held while you .join).

I wonder if this is a documentation issue, then. I might try finding a place to improve it, but if you have an idea how to go about that, I'll be glad to hear that.