What happens to a file object in Python when the process is terminated? Does it matter whether Python is terminated with
for i in $(seq 1 10); do
python termtest.py $i & export pypid=$!
kill -SIGTERM $pypid
end_loop = False
end_loop = True
with open('test' + str(sys.argv[-1]) + '.txt', 'w') as csvfile:
writer = csv.writer(csvfile)
for idx in range(int(1e7)):
writer.writerow((idx, 'a' * 60000))
It's not how files are "cleaned up" so much as how they are written to. It's possible that a program might perform multiple writes for a single "chunk" of data (row, or whatever) and you could interrupt in the middle of this process and end up with partial records written.
Looking at the C source for the
csv module, it assembles each row to a string buffer, then writes that using a single
write() call. That should generally be safe; either the row is passed to the OS or it's not, and if it gets to the OS it's all going to get written or it's not (barring of course things like hardware issues where part of it could go into a bad sector).
The writer object is a Python object, and a custom writer could do something weird in its
write() that could break this, but assuming it's a regular file object, it should be fine.