开发者

subprocess replacement of popen2 with Python

I tried to run this code from the book 'Python Standard Library' of 'Fr开发者_运维问答ed Lunde'.

import popen2, string

fin, fout = popen2.popen2("sort")

fout.write("foo\n")
fout.write("bar\n")
fout.close()

print fin.readline(),
print fin.readline(),
fin.close()

It runs well with a warning of

~/python_standard_library_oreilly_lunde/scripts/popen2-example-1.py:1: 
DeprecationWarning: The popen2 module is deprecated.  Use the subprocess module.

How to translate the previous function with subprocess? I tried as follows, but it doesn't work.

from subprocess import *

p = Popen("sort", shell=True, stdin=PIPE, stdout=PIPE, close_fds=True) 
p.stdin("foo\n")                #p.stdin("bar\n")


import subprocess
proc=subprocess.Popen(['sort'],stdin=subprocess.PIPE,stdout=subprocess.PIPE)
proc.stdin.write('foo\n')
proc.stdin.write('bar\n')
out,err=proc.communicate()
print(out)


Within the multiprocessing module there is a method called 'Pool' which might be perfect for your needs considering you are planning to do sort (not sure how huge the data is, but...).

It's optimizes itself to the number of cores your system has. i.e. only as many processes are spawned as the no. of cores. Of course this is customizable.

from multiprocessing import Pool

def main():
    po = Pool()
    po.apply_async(sort_fn, (any_args,), callback=save_data)
    po.close()
    po.join()
    return

def sort_fn(any_args):
    #do whatever it is that you want to do in a separate process.
    return data

def save_data(data):
    #data is a object. Store it in a file, mysql or...
    return
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜