views:

31

answers:

1
@cost_time
def dbdump_all():
    "导出数据库所有数据至当前目录下以年月日命名的sql文件"
    filename=datetime.datetime.now().strftime("%Y-%m-%d")
    cmd="""mysqldump -u root -pzhoubt --opt --quick --database search > ./%s.sql"""%filename
    args=shlex.split(cmd)
    p=subprocess.Popen(args)
    #stdout, stderr = p.communicate()
    #print stdout,stderr
    print "已将数据库表结构和数据导出到%s"%filename

I use the mysqldump command in a subprocess, and it outputs a lot of information about the exported data, even if I comment out the stdout, stderr = p.communicate() line. It's also very slow, even though I've tried the same command in a shell and it's very quick and succinct. How can I avoid all the verbosity when using subprocess, and speed up the time it takes to be more like running it directly from a shell?

A: 
@cost_time
def dbdump_all():
    "导出数据库所有数据至当前目录下以年月日命名的sql文件"
    filename=datetime.datetime.now().strftime("%Y-%m-%d")+".sql"
    cmd="""mysqldump -u root -pzhoubt --opt --quick --database search >./%s"""%filename
    print cmd
    p=subprocess.Popen(cmd,shell=True,cwd=os.getcwd())
    sts = os.waitpid(p.pid, 0)[1]
    print "返回状态%s"%sts
    print "已将数据库表结构和数据导出到%s"%filename

finally i got it, the key is us os.waitpid to wait mysql processing, another point is when you use shell the cmd is a string not a list

mlzboy