2

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()

create & read from tempfile

Community
  • 1
  • 1
bartosz
  • 404
  • 1
  • 3
  • 15

1 Answers1

1

Close the file after you write to it and before launching the secondary thread.

Paul Cornelius
  • 9,245
  • 1
  • 15
  • 24
  • Now I'm getting: `tar: Removing leading / from member names` `tar: /home/bartosz/Documents/serialized_mnaFP_.json: Cannot stat: No such file or directory` `tar: Exiting with failure status due to previous errors` and there is no file in the archive now, not even an empty one as previous – bartosz Jul 13 '16 at 09:00