Skip to content
Advertisement

how can i safely write to a file when using Flask?

I need to write a method on a Flask server which will write part of the request to a log file. However, if I understand correctly, Flask is multi-threaded, and there’s a good chance that writing to a file is not safe.

Admittedly, I am more or less new to Python and multi-threaded programming in general, so I need someone to somewhat hold my hand through this a little bit :)

My code so far (slightly modified, without the parts of that would get me in trouble for posting online)

@app.route('/store_test')
def store_test():
    now = str(time.time())
    ip_address = request.remote_addr
    agent = request.user_agent.string

    log_data = [now,ip_address,agent]
    log_data_string = "t".join(log_data)

    filename = "log.dat"

    f = open(filename,'a')
    f.write(log_data_string + "n")
    f.close()

    return 'OK'

I’m guessing I need to wrap some code around the open and close statements, but I have no real idea what and I’m reading the Threads and Processes chapter in “Python in a Nutshell” book, and it’s not really giving me much of an idea about how to actually use these methods.

Any help would be appreciated.

Advertisement

Answer

Use the logging module. More in general, you need to use a Lock

import threading

lock = threading.Lock()
...

with lock:
    #Open the file and write to it

Basically, this is what logging does as well. More precisely, the Handler-objects (that actually write to some output, e.g., a file) implement locking.

It is important that all processes use the same Lock object, instead of creating their own. Thus, you could put it on module-level or similar.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement