pipe large amount of data to stdin while using subprocess.Popen

后端 未结 10 635
花落未央
花落未央 2020-12-08 11:08

I\'m kind of struggling to understand what is the python way of solving this simple problem.

My problem is quite simple. If you use the follwing code it will hang. T

10条回答
  •  臣服心动
    2020-12-08 11:53

    Here's something I used to load 6G mysql dump file loads via subprocess. Stay away from shell=True. Not secure and start out of process wasting resources.

    import subprocess
    
    fhandle = None
    
    cmd = [mysql_path,
          "-u", mysql_user, "-p" + mysql_pass],
          "-h", host, database]
    
    fhandle = open(dump_file, 'r')
    p = subprocess.Popen(cmd, stdin=fhandle, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    
    (stdout,stderr) = p.communicate()
    
    fhandle.close()
    

提交回复
热议问题