views:

64

answers:

2

I have a Django app that opens a file, continuously reads it, and at the same time writes data to a Postgres database. My issue is that whenever I open a file,

file = open(filename, 'r')

I am unable to also create new things in the database,

Message.objects.create_message(sys, msg)

That should create a database entry with two strings. However, nothing seems to happen and I am presented with no errors :( If I decide to close the file, file.close(), before I write to the database everything is fine. My problem is that I need that file open to create my objects. Does anyone have a solution for this? Thanks.

EDIT

Here's some more of my code. Basically I have the following snippet following the end of a file and then writing to the database as it gets information.

file.seek(0,2)         
while True:
  line = file.readline()
  if not line:
    time.sleep(1)
    continue
  Message.objects.create_message(sys, line)

EDIT 2

Got this to work finally but I'm not sure why. I'd love to understand why this worked:

str1ng = line[0:len(line)-1]
Message.objects.create_message(sys, str1ng)

Some how there is a difference between that string and the string gathered from file.readline(). Any ideas?

+1  A: 

try this:

file = open(filename, 'r')
fileContents = file.read()
file.close()
Zonda333
That worked but I need to keep the file open since it is being added to while I am reading it.
William
The only thing I can think of is doing that every x seconds.
Zonda333
+1  A: 

Have you tried linecache? Something like this might work (not tested).

import linecache

i = 0
go = True
file = ...
while (go == True):
   out = linecache.getline(file,i)
   ...process out...
   i = i+1
   if i % 100 == 0:
       # check for cache update every 100 lines
       linecache.checkcache(file)
   if ( some eof condition):
       go = False
linecache.clearcache()
joel3000
Gave this approach a try but unfortunately the same problem occurs. Now I'm confused.
William
It appears as though your system can only have a single open file handle at a time. What are you using?
joel3000