JSON transfer not working between processes
Question: I am creating a board game, and long story short: I want a file that will
start up and set the main variables of the game (player name, board(array),
prompt, status - playing/gameover) and export these as a .json file. The set
up file is a large GIU file so I would like to run the main game (logic and
display) in a separate file.

I am practising doing this with a simple tic-tac-toe game, but for some reason
I cannot get the json file to export/import correctly (can't really tell
which) OR can't get an input function to work in the separate function.

(code is very basic and still incomplete, but I am simply trying to get the
first step to work - start up, ask for name, then run the file with a function
that will display the board and ask for the users next move)

solution 1) using subprocess.Popen

file1:

    
    
    import json, subprocess, os
    from distlib.compat import raw_input
    
    
    print('Welcome to TIC-TAC-TOE!')
    print()
    
    #Set up of the start information that is going to be passed as JSON payload and sent to each process back and forth
    #------------------------------------------------
    name = raw_input('Please enter your name: ')
    prompt = 'select a space: '
    board = [0,1,2,
         3,4,5,
         6,7,8]
    move = None
    status = 'playing'
    winner = None
    iter = 0
    #------------------------------------------------
    
    
    #JSON payload {dictionary} to be sent
    info = {'name':name, 'prompt':prompt, 'board':board, 'move':move, 'status': status, 'winner': winner, 'iter': iter, }
    
    print('START, info dictionary as it goes out: ', info)
    
    
    
    #file out which dumps info the info.json file
    fout = open('info.json', 'w')
    json.dump(info, fout)
    fout.close
    
    
    subprocess.Popen(["python3", "engineANDvisuals.py"])
    

file 2) 'engineANDvisuals.py'

    
    
    #imported libraries
    import random, funcs as f, json
    from distlib.compat import raw_input
    
    
    
    #front-end: iterface function
    def interface(jsonFile):
    
       fin = open(jsonFile, 'r')                                                        #open up the     json file to read
       info = json.load(fin)                                                             #load json file as info
       fin.close                                                                        #close the json file
    
       print('FRONT, info dictionary as it comes IN: ', info)
       print()
    
       name = info['name']                                                                  #set json-name to name 
       prompt = info['prompt']                                                      #set json-prompt to prompt   
       board = info['board']                                                            #set json-board to board
       winner = info['winner']                                                          #set json-winner to winner
       status = info['status']                                                          #set json-status to status      
    
    
       f.printBoard(board)                                                              #prints out the board so that the user can see it
       print(info['move'])
       info['move'] = input(f.returnName(name)+', please '+ f.returnPrompt(prompt))
    
       print('FRONT, info dictionary as it goes OUT: ', info)
    
       fout = open(jsonFile, 'w')                                                       #open up the json file to write
       json.dump(info, fout)                                                            #dumps new info into the file   
       fout.close                                                                       #closes the json file
    

The f.functions are simple print functions located in another file. With this
method I am getting all the way up to the input line in the second file, then
the program just stops. It doesn't terminate, my cores aren't running hard at
all (so I don't think it's in a loop) it just stops after the program asks for
the space.

method 2) now if I use os.system instead to open up the process it gives me a
huge error that I am taking to mean it is importing/exporting the json file
incorrectly.

    
    
    Traceback (most recent call last):
      File "engineANDvisuals.py", line 105, in <module>
        interface('info.json')
      File "engineANDvisuals.py", line 19, in interface
        info = json.load(fin)                                                                #load json file as info
      File "/usr/lib/python3.4/json/__init__.py", line 268, in load
        parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
      File "/usr/lib/python3.4/json/__init__.py", line 318, in loads
        return _default_decoder.decode(s)
      File "/usr/lib/python3.4/json/decoder.py", line 343, in decode
        obj, end = self.raw_decode(s, idx=_w(s, 0).end())
      File "/usr/lib/python3.4/json/decoder.py", line 361, in raw_decode
        raise ValueError(errmsg("Expecting value", s, err.value)) from None
    ValueError: Expecting value: line 1 column 1 (char 0)
    

I am so insanely confused because when I run the second file alone after the
first it works perfectly.

I would really appreciate some help, I know it's probably some silly overlook,
a noob could really use some help, thank you so much.


Answer: Short answer : `fout.close` is missing the parens - you want `fout.close()` \-
or better use a `with` statement:

    
    
    with open('info.json', 'w') as fout:
        json.dump(info, fout)
    

Longer answer:

Without the parens, `fout.close` evals to the `close` method of `fout`, but
the method is not called:

    
    
    >>> f = open("foo.txt", "w")
    >>> print f.close
    <built-in method close of file object at 0xf0d660>
    

Since the file is not closed, the buffer is not flushed to disk, so the
subprocess cannot read it's content.

Once the main process ends, the file object is closed at garbage collection
time and the buffer is flushed to disk, so if you execute the second script on
it's own at this time it does read the file content.

