I have a WSGI application on apache2, that writes to a log file in the simplest of manners:
def log (msg):
with open (LOGFILE, 'a') as f: f.write (msg)
In my sandbox environment it works fine, nevertheless I am a bit concerned about concurrency. If apache2 runs various threads, do I have to fear concurrency problems? Will maybe my log file get scrambled? Will perhaps calls to log (msg) fail if another thread is already logging? If this were the case, how would I prevent it?
EDIT:
For testing, I run two scripts at the same time from two shells:
#! /usr/bin/python3.2
def log ():
with open ('log', 'a') as f:
f.write ('message from thread A\n')
while (True): log ()
and
#! /usr/bin/python3.2
def log ():
with open ('log', 'a') as f:
f.write ('message from thread B\n')
while (True): log ()
The log file looks good and no error occurred. Was I lucky or is it safe to write to the same file from two different threads. Filesystem is ext4.
You can use the python logging module