I'm pretty sure one would do this using the os.plock(op) function, but I have no idea how. Also, if there's a better way, I'd be grateful to find out. Code snippets are very welcome.
我很确定使用os.plock(op)函数可以做到这一点,但我不知道怎么做。此外,如果有更好的方法,我很高兴找到答案。代码片段非常受欢迎。
5 个解决方案
#1
12
Subprocess replaces os.popen, os.system, os.spawn, popen2 and commands. A simple example for piping would be:
子进程替换os.popen,os.system,os.spawn,popen2和命令。管道的一个简单示例是:
p1 = Popen(["dmesg"], stdout=PIPE)
p2 = Popen(["grep", "hda"], stdin=p1.stdout, stdout=PIPE)
output = p2.communicate()[0]
You could also use a memory mapped file with the flag=MAP_SHARED for shared memory between processes.
您还可以使用带有标志= MAP_SHARED的内存映射文件来处理进程之间的共享内存。
multiprocessing abstracts both pipes and shared memory and provides a higher level interface. Taken from the Processing documentation:
多处理抽象管道和共享内存,并提供更高级别的接口。取自Processing文档:
from multiprocessing import Process, Pipe
def f(conn):
conn.send([42, None, 'hello'])
conn.close()
if __name__ == '__main__':
parent_conn, child_conn = Pipe()
p = Process(target=f, args=(child_conn,))
p.start()
print parent_conn.recv() # prints "[42, None, 'hello']"
p.join()
#2
7
Take a look at the multiprocessing module new in python 2.6 (also available for earlier versions a pyprocessing
看一下python 2.6中新增的多处理模块(也可用于早期版本的pyprocessing)
Here's an example from the docs illustrating passing information using a pipe for instance:
以下是使用管道传递信息的文档中的示例:
from multiprocessing import Process, Pipe
def f(conn):
conn.send([42, None, 'hello'])
conn.close()
if __name__ == '__main__':
parent_conn, child_conn = Pipe()
p = Process(target=f, args=(child_conn,))
p.start()
print parent_conn.recv() # prints "[42, None, 'hello']"
p.join()
#3
4
This is pretty much Python-independent! It's a classic example of Unix interprocess communication. One good option is to use popen()
to open a pipe between the parent and child processes, and pass data/messages back and forth along the pipe.
这几乎与Python无关!这是Unix进程间通信的典型例子。一个好的选择是使用popen()在父进程和子进程之间打开管道,并沿管道来回传递数据/消息。
Take a look at the subprocess
module, which can set up the necessary pipes automatically while spawning child processes.
看一下子进程模块,它可以在生成子进程时自动设置必要的管道。
#4
1
You have two options: os.popen*
in the os
module, or you can use the subprocess
module to the same effect. The Python manual has pretty documentation and examples for popen and subprocess.
您有两个选项:os模块中的os.popen *,或者您可以使用子进程模块达到相同的效果。 Python手册有很多文档和popen和subprocess的例子。
#5
0
If you are doing low-level operating system forking and really want to avoid using pipes, it is possible to use shared memory-mapped files as well. This is not nearly as nice as using subprocess
or popen
pipes, but including the answer for completeness...
如果您正在进行低级操作系统分叉并且确实希望避免使用管道,则也可以使用共享内存映射文件。这不如使用子进程或popen管道那么好,但包括完整性的答案......
There's a full example here, but basically you can combine the os file handling and mmap modules:
这里有一个完整的例子,但基本上你可以结合os文件处理和mmap模块:
import mmap, os, tempfile
fd, tmpfile = tempfile.mkstemp()
os.write(fd, '\x00' * mmap.PAGESIZE)
os.lseek(fd, 0, os.SEEK_SET)
child_pid = os.fork()
if child_pid:
buf = mmap.mmap(fd, mmap.PAGESIZE, mmap.MAP_SHARED, mmap.PROT_READ)
os.waitpid(child_pid, 0)
child_message = buf.readline()
print(child_message)
os.close(fd)
else:
buf = mmap.mmap(fd, mmap.PAGESIZE, mmap.MAP_SHARED, mmap.PROT_WRITE)
buf.write('testing\n')
os.close(fd)
#1
12
Subprocess replaces os.popen, os.system, os.spawn, popen2 and commands. A simple example for piping would be:
子进程替换os.popen,os.system,os.spawn,popen2和命令。管道的一个简单示例是:
p1 = Popen(["dmesg"], stdout=PIPE)
p2 = Popen(["grep", "hda"], stdin=p1.stdout, stdout=PIPE)
output = p2.communicate()[0]
You could also use a memory mapped file with the flag=MAP_SHARED for shared memory between processes.
您还可以使用带有标志= MAP_SHARED的内存映射文件来处理进程之间的共享内存。
multiprocessing abstracts both pipes and shared memory and provides a higher level interface. Taken from the Processing documentation:
多处理抽象管道和共享内存,并提供更高级别的接口。取自Processing文档:
from multiprocessing import Process, Pipe
def f(conn):
conn.send([42, None, 'hello'])
conn.close()
if __name__ == '__main__':
parent_conn, child_conn = Pipe()
p = Process(target=f, args=(child_conn,))
p.start()
print parent_conn.recv() # prints "[42, None, 'hello']"
p.join()
#2
7
Take a look at the multiprocessing module new in python 2.6 (also available for earlier versions a pyprocessing
看一下python 2.6中新增的多处理模块(也可用于早期版本的pyprocessing)
Here's an example from the docs illustrating passing information using a pipe for instance:
以下是使用管道传递信息的文档中的示例:
from multiprocessing import Process, Pipe
def f(conn):
conn.send([42, None, 'hello'])
conn.close()
if __name__ == '__main__':
parent_conn, child_conn = Pipe()
p = Process(target=f, args=(child_conn,))
p.start()
print parent_conn.recv() # prints "[42, None, 'hello']"
p.join()
#3
4
This is pretty much Python-independent! It's a classic example of Unix interprocess communication. One good option is to use popen()
to open a pipe between the parent and child processes, and pass data/messages back and forth along the pipe.
这几乎与Python无关!这是Unix进程间通信的典型例子。一个好的选择是使用popen()在父进程和子进程之间打开管道,并沿管道来回传递数据/消息。
Take a look at the subprocess
module, which can set up the necessary pipes automatically while spawning child processes.
看一下子进程模块,它可以在生成子进程时自动设置必要的管道。
#4
1
You have two options: os.popen*
in the os
module, or you can use the subprocess
module to the same effect. The Python manual has pretty documentation and examples for popen and subprocess.
您有两个选项:os模块中的os.popen *,或者您可以使用子进程模块达到相同的效果。 Python手册有很多文档和popen和subprocess的例子。
#5
0
If you are doing low-level operating system forking and really want to avoid using pipes, it is possible to use shared memory-mapped files as well. This is not nearly as nice as using subprocess
or popen
pipes, but including the answer for completeness...
如果您正在进行低级操作系统分叉并且确实希望避免使用管道,则也可以使用共享内存映射文件。这不如使用子进程或popen管道那么好,但包括完整性的答案......
There's a full example here, but basically you can combine the os file handling and mmap modules:
这里有一个完整的例子,但基本上你可以结合os文件处理和mmap模块:
import mmap, os, tempfile
fd, tmpfile = tempfile.mkstemp()
os.write(fd, '\x00' * mmap.PAGESIZE)
os.lseek(fd, 0, os.SEEK_SET)
child_pid = os.fork()
if child_pid:
buf = mmap.mmap(fd, mmap.PAGESIZE, mmap.MAP_SHARED, mmap.PROT_READ)
os.waitpid(child_pid, 0)
child_message = buf.readline()
print(child_message)
os.close(fd)
else:
buf = mmap.mmap(fd, mmap.PAGESIZE, mmap.MAP_SHARED, mmap.PROT_WRITE)
buf.write('testing\n')
os.close(fd)