如何用python编写多进程文件

2024-05-08 20:09:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在每次启动程序时向文件写入进程id,所以我写了这段代码,但这段代码在进程停止时就将pid写入文件。在

from multiprocessing import Process
from os import getpid


def f():
    file = open('udp.pid', 'w')
    file.write(str(getpid()))
    file.close()
    while True:
        # a socket infinity loop
        pass

if __name__ == '__main__':
    p = Process(target=f,)
    p.start()

如何在多进程中将pid写入文件?在

更新:

我在Windows8.1上使用Python3.4


Tags: 文件代码fromimport程序id进程os
1条回答
网友
1楼 · 发布于 2024-05-08 20:09:27

我认为是否使用多进程并不重要。只是为了写作!在

打开(name[,mode[,buffering]])

https://docs.python.org/2/library/functions.html

The optional buffering argument specifies the file’s desired buffer size: 0 means unbuffered, 1 means line buffered, any other positive value means use a buffer of (approximately) that size (in bytes).

更改您的代码

file = open('udp.pid', 'w')

^{pr2}$

相关问题 更多 >