简体   繁体   English

可以为子进程 (Windows) 抑制 Python 版本消息吗?

[英]Can Python version message be suppressed for child processes (Windows)?

I am using multiprocessing to calculate a large mass of data;我正在使用multiprocessing来计算大量数据; ie I periodically spawn a process so that the total number of processes is equal to the number of CPU's on my machine.即我定期生成一个进程,以便进程总数等于我机器上的 CPU 数量。 I periodically print out the progress of the entire calculation... but this is inconveniently interspersed with Python's welcome messages from each child!我定期打印出整个计算的进度……但这不方便地穿插在每个孩子的 Python 的欢迎信息中! To be clear, this is a Windows specific problem due to how multiprocessing is handled.需要明确的是,由于多处理的处理方式,这是Windows 特定的问题

Eg例如

> python -q my_script.py

Python Version: 3.7.7 on Windows

Then many subsequent duplicates of the same version message print;然后打印出许多相同版本消息的后续副本; one for each child process.每个子进程一个。 How can I suppress these?我怎样才能抑制这些?

I understand that if you run Python on the command line with a -q flag, it suppresses the welcome message;我知道如果您在命令行上使用-q标志运行 Python,它会抑制欢迎消息; though I don't know how to translate that into my script.虽然我不知道如何将它翻译成我的脚本。

EDIT:编辑:

I tried to include the interpreter flag -q like so:我试图像这样包含解释器标志-q

multiprocessing.set_executable(sys.executable + ' -q')

Yet to no avail.却无济于事。 I receive a FileNotFoundError which tells me I cannot pass options this way due to how they check arguments.我收到一个FileNotFoundError ,它告诉我由于它们检查参数的方式,我不能以这种方式传递选项。

Anyways, here is the relevant section of code (It's an entire function):无论如何,这是代码的相关部分(这是一个完整的功能):

def _parallelize(self, buffer, func, cpus):
    ## Number of Parallel Processes ##
    cpus_max = mp.cpu_count()
    cpus = min(cpus_max, cpus) if cpus else int(0.75*cpus_max)

    ## Total Processes to-do ##
    N = ceil(self.SampleLength / DATA_MAX)  # Number of Child Processes
    print("N: ", N)
    q = mp.Queue()  # Child Process results Queue

    ## Initialize each CPU w/ a Process ##
    for p in range(min(cpus, N)):
        mp.Process(target=func, args=(p, q)).start()

    ## Collect Validation & Start Remaining Processes ##
    for p in tqdm(range(N)):
        n, data = q.get()  # Collects a Result

        i = n * DATA_MAX  # Shifts to Proper Interval

        buffer[i:i + len(data)] = data  # Writes to open HDF5 file

        if p < N - cpus:  # Starts a new Process
            mp.Process(target=func, args=(p + cpus, q)).start()

SECOND EDIT:第二次编辑:

I should probably mention that I'm doing everything within an anaconda environment.我可能应该提到我在 anaconda 环境中做所有事情。

The message is printed on interactive startup.该消息在交互式启动时打印。

A spawned process does inherit some flags from the child process.生成的进程确实从子进程继承了一些标志。 But looking at the code in multiprocessing it does not seem possible to change these parameters from within the program.但是查看multiprocessing中的代码似乎不可能从程序内部更改这些参数。

So the easiest way to get rid of the messages should be to add the -q option to the original python invocation that starts your program.因此,摆脱消息的最简单方法应该是将-q选项添加到启动程序的原始python调用中。 I have confirmed that the -q flag is inherited.我已经确认-q标志是继承的。

So that should suppress the message for the original process and the children that it spawns.因此,这应该会抑制原始进程及其产生的子进程的消息。

Edit: If you look at the implementation of set_executable , you will see that you cannot add or change arguments that way.编辑:如果您查看set_executable的实现,您会发现您不能以这种方式添加或更改参数。 :-( :-(

Edit2: You wrote:编辑2:你写道:

I'm doing everything within an anaconda environment.我在 anaconda 环境中做所有事情。

You you mean a virtual environment, or some kind of fancy IDE like spyder?你是说虚拟环境,还是像 spyder 这样的花哨的 IDE?

If you ever have a Python problem, first try reproducing it in plain CPython , running from the command line.如果您遇到Python 问题,请首先尝试使用普通的 CPython重现它,从命令行运行。 IDE's and fancy environments like anaconda sometimes do weird things when running Python. IDE 和 anaconda 之类的花哨环境在运行 Python 时有时会做一些奇怪的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM