1

I'm trying to stop a script from outputting text to the command line. It uses selenium in combination with chromedriver and chrome. I am using multiprocessing which could be relevant to the problem.

I've tried starting from command line in windows with this:

python foo.py 1>nul

This redirects stdout to nothing and works on my own print statements, but i still get output from chromedriver (or chrome). I have tried redirecting all the numerical handles from 0-9 but that doesn't help.

What can i do to silence the modules i am importing? I would prefer being able to remove the clutter while keeping my own ability to print to screen, but right now i'd take anything.

Output examples:

DevTools listening on ws://127.0.0.1:12818/devtools/browser/089e9b8d-66ce-431c-8486-abf1db2e1bc1
[10752:15088:1216/122840.419:ERROR:shader_disk_cache.cc(238)] Failed to create shader cache entry: -2
[27752:21264:1216/123032.960:ERROR:process_metrics.cc(105)] NOT IMPLEMENTED

I have also tried contextlib.redirect_stdout but this doesn't work on subprocesses and i spawn multiple via multiprocessing module.

raecer
  • 195
  • 4
  • 14
  • 2
    Are you sure it is not stderr? You said you redirected all handles 0-9 (post the command lines you used?), but perhaps checking the documentation for chromedriver... You may post the specific output you get, that may help others see. – sancho.s ReinstateMonicaCellio Dec 16 '17 at 11:21
  • Quite sure, but like i said, i tried redirecting stderr as well. I tried a quite extreme amount of command line variants from manually redirecting them all, separately redirecting them to bunching them together via 2>&1 etc. Doesn't seem to be related to that part. I posted some output examples. – raecer Dec 16 '17 at 11:34
  • 2
    Certainly not attacking your problem, but perhaps useful... https://stackoverflow.com/questions/47392423/python-selenium-devtools-listening-on-ws-127-0-0-1 – sancho.s ReinstateMonicaCellio Dec 16 '17 at 11:54
  • Sorry, i didn't mean to come off as stand-offish. Appreciate any help. I'm running the newest version of chromedriver and chrome and i have tried all kinds of settings for those respective products. Some work for a while but they all end up not working eventually due to their development however. I'm just tired of all the messages that never actually affect the outcome of my script so i'd just like to mute them permanently for now. – raecer Dec 16 '17 at 12:02
  • 1
    Start with how this works. Shell file numbers 0-9 correspond to the file descriptor numbers of the shell's C runtime library (CRT). A valid file descriptor is mapped to a handle that references a kernel File object. A process uses a single handle table for all references to kernel objects (e.g. Process, Thread, Section, File, Event, etc), whereas CRT file descriptors are limited to just files. A process has three standard I/O handles -- `StandardInput`, `StandardOutput`, and `StandardError`, which should be File handles. The CRT tries to keep them in sync with file descriptors 0, 1, and 2. – Eryk Sun Dec 16 '17 at 20:43
  • 1
    If you redirect `1 > NUL`, that sets `StandardOutput` to a handle for the "NUL" device (i.e. native NT "\Device\Null"), which is inherited by a child process. But there's also a `ConsoleHandle` value that's inherited by child console applications, so they can share the parent's console. Windows supports special "CON", "CONIN$", and "CONOUT$" files to open a new handle to the console via this internal console connection. Thus an application can write to the console by opening "CON" or "CONOUT$", regardless of its inherited standard handles. – Eryk Sun Dec 16 '17 at 20:52
  • I'll try to wrap my head around this tomorrow when i've rested :) Are you suggesting it is possible to block the inheritance of the consolehandles or their ability to write to console? Or will i have to look elsewhere? I'll read up on your suggestions either way since my knowledge is obviously lacking in these areas. – raecer Dec 16 '17 at 21:06
  • I would suggest you also post the contents of `foo.py` responsible for the undesired output, with related `import`s, etc. – sancho.s ReinstateMonicaCellio Dec 16 '17 at 22:22
  • 1
    @raecer, I was just explaining how it works in general. But yes, you can prevent inheriting the console or force a child to allocate a new console that either has a hidden window or no window. It depends on how the child process is created. – Eryk Sun Dec 17 '17 at 10:43

1 Answers1

0

I think that you need to configure logging in your python script.

See https://stackoverflow.com/a/21688906/292787 for how to setup logging for selenium.

And see https://stackoverflow.com/a/14058475/292787 for how to redirect logs to stdout.

Stanislav
  • 4,389
  • 2
  • 33
  • 35