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)

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

    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 its not really giving me much of an idea about how to actually use these methods.

any help would be appreciated.

1/30/2017 6:44:51 PM

Accepted 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.

1/10/2013 12:42:22 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow