Difficulty writing one-shot clients

I find myself writing one-shot command line clients in python from time to time and autobahn seems not to be tuned well to that use case. It really wants to be a long running service.

My immediate problem is that txaio is taking over stdout. This is immediately pressing because I’ve been tasked with writing a nagios plugin that is a wamp client. Thus, the application must adhere to a strict format on stdout. I can’t find a way to accomplish that.

Problems encountered:

  • autobahn/twisted logs are output by default. I’ve tried txaio.start_logging(level='warn'), but that silences my own print statemetns!
  • autobahn/twisted logs are sent to stdout. I’ve tried txaio.start_logging(sys.stderr), but that redirects my own print statements to stderr!
  • my own print statements are reformatted per txaio’s log format (datetime prefixed). That’s neat, but my application must not do that.

Practically, I find this frustrating because I have to deliver working code and this fabulous library that works so well in other projects now has me at a hard stop.

Conceptually, I find this frustrating because, for a command line program at least, stdout is an application concern, not a library concern. I don’t think any library should be this invasive in its instrumentation of stdout and stderr. Especially in a way that my application cannot adjust.

I’ve checked the txaio docs and didn’t find anything (FWIW start_logging() isn’t even doc’ed there). I’ve checked the code and I can’t figure out how it even takes over the print() global function. Please show me how to adjust txaio so that my application exclusively controls stdout.

More generally, it’s not obvious in the docs for autobahn-python what the Right Way is to write a one-shot client. I found and generally use Component(main=my_main_func) with is_fatal=lambda _: True to avoid auto-reconnecting. Maybe I’m missing something about is_fatal but I can’t seem to handle transport connection failures at the application level. I’d like to inform my users of failures to connect to the websocket in a way that matches other edge case output from my application, not in the way that txaio wants to produce logs. Seems impossible from what I can tell.

And to be clear, I like the attributes of autobahn/txaio that I just complained about… when I’m writing a long running service. The logging and aggressive auto-reconnect out of the box are great for that use case. I just think that autobahn can and should address both use cases well.

Since writing this I found a workaround: stdout, stderr = sys.stdout, sys.stderr before importing anything autobahn/twisted/txaio related.