1

I have a test suite harness which is used to run test scripts (classes defined therein actually), and as it iterates through the tests, it manipulates the python logger such that the log messages are all output to different files, each associated with its own test (class). This works fine for tests run in a sequential manner where i can control the log handlers in the root logger which enable all log messages (from whatever libraries the test classes may use) to log their messages into the proper test log file.

But what I am really trying to figure out is how to run such tests in parallel (via threading or multiprocessing) such that each thread will have its own log file to place all such messages.

I believe that I still need to manipulate the root logger, because that is the only place both tests and the libraries they use will converge on to do all logging to a common place.

I was thinking that I could add a handler for each thread which would contain a log filter to only log from a particular thread, and that would get me close (haven't tried this yet, but seems possible in theory). And this would possibly be the full solution (if indeed such would work) except for one thing. I cannot tell test writers to not use threads themselves, in their tests. So if they did so, again, this solution would fail. I'm fine with test-internal threads all logging to the one file, but these new threads would fail to log to the file their parent thread is logging to. The filter doesn't know anything about them.

And I could be mistaken, but it seems that threading.Thread objects cannot determine their own parent thread? This precludes a better log handler filter that accepts messages generated in a thread or any of its child/descendant threads. (?)

Any suggestions about how to approach this would be great.

Thanks,

Bruce

Bruce
  • 25
  • 3
  • You could assign a logger with a different name to each thread. Configuring the loggers should be easy. – Mad Physicist Jan 05 '19 at 02:09
  • You could spawn a new process for each test. These can then be spawned in parallel and you just need to wait until they've completed. This removes the need to try and make the logging mechanism smart. – Claudio Corsi Jan 05 '19 at 22:13
  • @ Mad Physicist: These tests access independent libraries which also need to write to the log, and they are invoked (as seems standard) using the name of their package. Such logging would never see this test based logger. The only place that both test and library converge re loggers is at the root logger. This is why I am creating handlers in the root logger. – Bruce Jan 07 '19 at 16:22
  • @Claudio Corsi: I tried a small test using the multiprocessing package, where I create 4 Process objects and then ask them who their root logger is. All four of them show they are using the same root logger instance (). I agree this does not sound intuitively correct, but unless these are somehow not real, independent processes (which I have to say, it seems they are not), then the multiple process idea either needs to be done in another manner, or the same root logger is used by everything that accesses logging within a single python invocation. – Bruce Jan 07 '19 at 16:33
  • @Mad Physicist: To clarify- this: "and they are invoked (as seems standard) using the name of their package" should have read thusly: "and the library objects' access to logging is invoked (as seems standard) using the name of their package" – Bruce Jan 07 '19 at 17:51
  • @Bruce: Have you tried the subprocess library instead? – Claudio Corsi Jan 07 '19 at 20:51
  • @Claudio Corsi: No, I haven't. But to use subprocess, everything I want to parallelize would need to be a standalone command line executable, which would be a fairly serious rewrite. But maybe it is possible. Each of them would seem to invoke the python interpreter separately, so I'd say there is a good chance each would have its own root logger, and would make this problem less intractable. p.s. slightly ironic if "sub"process actually gives more independent process entities than "multi"processing does... ;-) – Bruce Jan 08 '19 at 20:39
  • Here's a resolution: https://stackoverflow.com/questions/22643337/per-thread-logging-in-python/55035193#55035193 – Shawn Hu Mar 07 '19 at 03:17

0 Answers0