python ThreadPoolExecutor 获取进程名称
时间: 2023-10-21 18:27:25 浏览: 200
您可以使用`multiprocessing.current_process().name`来获取线程池中当前正在运行的线程的进程名称。下面是一个示例代码:
```python
import concurrent.futures
import multiprocessing
def worker():
process_name = multiprocessing.current_process().name
print(f"Current process name: {process_name}")
def main():
with concurrent.futures.ThreadPoolExecutor() as executor:
executor.submit(worker)
executor.submit(worker)
executor.submit(worker)
if __name__ == '__main__':
main()
```
输出结果将类似于:
```
Current process name: ThreadPoolExecutor-0_0
Current process name: ThreadPoolExecutor-0_1
Current process name: ThreadPoolExecutor-0_2
```
这里,`ThreadPoolExecutor-0_0`,`ThreadPoolExecutor-0_1`和`ThreadPoolExecutor-0_2`是线程池中的三个线程的进程名称。
阅读全文