I'm having troubles writing asynchronous I/O program. What I'm trying to achieve is: dump json data into a temporary file so I can then use subprocess to create an archive of that file (with json data). However I figured out that I'm trying to tar an empty file from tempfile.NamedTemporatyFile
.
serialized_data = {'a': 1}
temp_file = tempfile.NamedTemporaryFile(dir='.', prefix='serialized_', suffix='.json')
temp_file.write(json.dumps(serialized_data))
arch_name = temp_file.name + '.tar.gz'
tar_cmd_args = ['tar', '-czf', arch_name, temp_file.name]
subprocess.call(tar_cmd_args)
#d = threads.deferToThread(subprocess.call, tar_cmd_args)
I'm trying to avoid I/O as much as possible since I don't want to block callback chain and I have to be sure those operations are thread safe.
Or is another way of solution to my problem? I really want to avoid json.dump(serialized, file_name)
[edit]
While temp_file.close()
caused:
tar: /home/bartosz/Documents/serialized_mnaFP_.json: Cannot stat: No such file or directory tar: Exiting with failure status due to previous errors
The solution was no to close the file, but to flush.
temp_file.flush()