Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 660
badideas Avatar asked Jan 10 '13 12:01

badideas


People also ask

How do you handle a flask file?

Handling file upload in Flask is very easy. It needs an HTML form with its enctype attribute set to 'multipart/form-data', posting the file to a URL. The URL handler fetches file from request. files[] object and saves it to the desired location.

Is flask safe for production?

Although Flask has a built-in web server, as we all know, it's not suitable for production and needs to be put behind a real web server able to communicate with Flask through a WSGI protocol.


1 Answers

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.

like image 78
Thorsten Kranz Avatar answered Nov 15 '22 00:11

Thorsten Kranz