获取Python Pebble库中生成的进程的进程ID?

时间:2020-10-08 00:17:53

标签: python concurrency multiprocessing concurrent.futures

我正在使用Pebble库中的ProcessPool启动易于崩溃的子进程。我想记录崩溃的子流程的process-id,但是从主流程而不是子流程中记录(原因是我在主流程中有一条日志行,其中有一堆与一个请求相关的相关信息,其中我想包括它而不是分散在多个日志行中。有什么方法可以访问这个进程ID?我似乎在文档中找不到此信息。

我想作为一种解决方法,我可以在使用os.getpid()做任何事情之前先在子进程中获取pid,然后使用IPC将其传达回父进程。但我想避免这种情况。

1 个答案:

答案 0 :(得分:1)

import React, { useState } from "react"; import "./styles.css"; export default function App() { const [cities, setCities] = useState(["Pune", "Delhi"]); const [name, setName] = useState(""); const handleSubmit = (event) => { event.preventDefault(); if (!name) { alert("Enter the city name"); return; } let tempList = cities.filter( (city) => city.toLowerCase() !== name.toLowerCase() ); if (tempList.length === cities.length) { tempList.push(name); setCities(tempList); return; } else { setCities(tempList); } }; return ( <div className="App"> <form onSubmit={handleSubmit}> <input onChange={(event) => setName(event.target.value)} /> <button type="submit">Submit</button> </form> {cities.map((city) => ( <p>{city}</p> ))} </div> ); } 旨在从用户那里提取其内部工作原理。因此,它隐藏了对用于执行工作程序的进程的访问。

如果仅出于调试目的而需要此信息,我的建议是使用唯一的标识符标记您的作业,然后从工作进程中记录这些标识符和工作进程PID。这样,您可以关联导致功能崩溃的工作。

handleCityCheckbox = (param1) => {
    const { cityList = [] } = this.state;
    const itemIndex = cityList.indexOf(param1);
    if (itemIndex === -1)) {
      cityList.push(param1);
    } else  {
      cityList = cityList.filter((e, index) => index !== itemIndex)
    }
    this.setState({ cityList });
  };
相关问题