Reputation: 1638
I'm having an issue with my websocket script. Over time it consumes more and more CPU. One remedy I've discovered is to clear the associated logfile. This resolves the problem for a little while, but the CPU usuage builds up to 120% in little over a day or so. (using the top command on the linux server)
The part of the script that does the file write looks a bit odd to me. Here is the code:
f = open(file, 'a')
f.write(line+"\n")
os.fsync(f.fileno())
f.flush()
f.close
I'm not a Python expert, but for starters, the last three things do rather the same in my opinion. The python manual states http://docs.python.org/2/library/os.html#os.fsync that f.flush and os.fsync should be in reverse order...
Can I just use:
f = open(file, 'a')
f.write(line+"\n")
f.close
and should it not be: f.close()??
Any ideas?
Upvotes: 3
Views: 180
Reputation: 213115
Use this to open (and automatically close) your files:
with open(filename, 'a') as f:
f.write(line+"\n")
Upvotes: 4