[英]Python: Storing a mysql dump using mysqldump + gridfs
I'm trying to make a python script that (in order)我正在尝试制作一个 python 脚本(按顺序)
/tmp/x.sql
/tmp/x.sql
中的文件中 Motivation : Many different versions of a database for testing动机:许多不同版本的数据库用于测试
My problem : Step 1 does not complete writing to the file before Step 2 runs, hence an empty file is read我的问题:第 1 步未在第 2 步运行之前完成对文件的写入,因此读取了一个空文件
What I've got我有什么
# filename is a generated uuid4
def step_1(filename):
d = dict(environ)
d["MYSQL_PWD"] = "root_pass"
file = open(filename, "wb")
subprocess.Popen(
["mysqldump", "-h", "0.0.0.0", "-u", "root", "database"], stdout=file, env=d
)
def step_2(filename):
with open(filename, "rb") as f:
file = f.read() <- This is always empty
mongo_gridfs.put(data, content_type = 'file/txt')
mongo_gridfs.save()
Right now my "solution" is just calling sleep(1)
现在我的“解决方案”只是调用
sleep(1)
There doesn't seem to be a method in the file
that tells you if it's being written or if writing has completed, so I'm just kinda looking for a way to know if the file has been written so that I don't have to call sleep(1)
file
中似乎没有一种方法可以告诉您文件是否正在写入或写入是否已完成,所以我只是在寻找一种方法来了解文件是否已写入,这样我就没有了打电话sleep(1)
If there is a way to pipe this directly into a ByteIO
object and read that into gridfs that would be even better如果有办法将 pipe 直接放入
ByteIO
object 并将其读入 gridfs,那会更好
Well found two excellent answers in 1 minutes好在 1 分钟内找到了两个很好的答案
doing正在做
process = subprocess.Popen(
["mysqldump", "-h", "0.0.0.0", "-u", "root", "s2r-db"], stdout=PIPE, env=d
)
process.wait() <- add this
made it work, but also adding PIPE
to the stdout
and reading it into a BytesIO
instead of writing a file使其工作,但也将
PIPE
添加到stdout
并将其读入BytesIO
而不是写入文件
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.