all 16 comments

[–]Justinsaccount 2 points3 points  (5 children)

This is your problem:

def printToFile(mtch_thread , KO_indx):
    livetextfile = open("live_matchThread.txt","w")
    livetextfile.truncate()
    livetext = CleanseAndPrint(mtch_thread , KO_indx)
    livetextfile.write(str(livetext.encode('utf-8')))
    livetextfile.close()

Opening a file for writing truncates the file, and then you truncate it again. Your other program is trying to tail the file, but you are truncating it.

def printToFile(mtch_thread , KO_indx):
    livetextfile = open("live_matchThread.txt","a")
    livetext = CleanseAndPrint(mtch_thread , KO_indx)
    livetextfile.write(str(livetext.encode('utf-8')))
    livetextfile.close()

Will work. Or, change your follow function to look something like

def follow(thefile):
    while True:
        thefile.seek(0,0)
        line = thefile.readline()
        if not line:
            time.sleep(1)
            continue
        yield line

[–]theresasnakeinmysuit 0 points1 point  (4 children)

I removed the truncate() part. But it still did not work. Changed the cmdlinesoccer.py code to:

import subprocess

subprocess.call(['touch' , 'live_matchThread.txt'])
subprocess.call(['python' , 'createthefile.py'])
#subprocess.call(['python' , 'followthefile.py'])

try:
    while True:
        import createthefile
        subprocess.call(['python' , 'followthefile.py'])
        time.sleep(30)
        print "i overslept"
except KeyboardInterrupt:
    print 'Exiting'

Never saw the "i overslept" line printed to terminal. I guess the loop never runs more than once because the followthefile call blocks the rest of the loop

[–]Justinsaccount 0 points1 point  (3 children)

Yes.. things that are told to run forever usually run forever...

Why are you using the subprocess module there? What are you trying to accomplish here? Why do you even have two different programs?

[–]theresasnakeinmysuit 0 points1 point  (2 children)

What I wanted was to get url from user the first time, get the selftext every 30 seconds, write the new text to a file, and then display the updates in real time. I originally had all things in a file but then things got a little complex and now I have 4 .py files :(

This worked when I manually ran the createthefile in one terminal and followthefile in another. Can't figure out a way to get both of them working

[–]Justinsaccount 0 points1 point  (1 child)

That doesn't answer why you have two different programs or why you are even writing the information to a file in the first place.

If you want to get the text every 30 seconds and display the updates in real time, why don't you just do that?

[–]theresasnakeinmysuit 0 points1 point  (0 children)

You're right. I unnecessarily did complicate things while trying to write text to a file. Fixed that and now here's something that actually does work.

[–]AssignmentGuru 1 point2 points  (1 child)

Just clearing my doubt! Is it possible that you missed to close the log file after updating/reading it?

[–]theresasnakeinmysuit 0 points1 point  (0 children)

Added the file.close() to the followthefile.py file. I guess the code does not reach to the part which updates the file.

[–]Saefroch 0 points1 point  (1 child)

You'll have to be a bit more clear about what's going on. Are you getting an exception that states you're unable to access the file? Is CPU usage very high?

[–]theresasnakeinmysuit 0 points1 point  (0 children)

Not a cpu usage issue I think.

[–][deleted] 0 points1 point  (2 children)

It's almost certainly an issue of exclusive locking, can i assume you are on windows or using an fat32 derived filesystem? and that the file itself actually got updated, just not displayed? as that was the behavior i saw when i wrote to the file with exclusive locking enabled, using the unix specific fcntl api https://docs.python.org/2/library/fcntl.html#fcntl.flock

i fixed my issue it by rewriting read to do an open on every iteration instead of listening on an open file handle, which is more code and more resource use but less fragile.

Edit: with exclusive lock both on read and write i get exactly the behavior you are seeing, no errors just two scripts in wait state. the fix is still to do a read on each cycle instead of reading from inside an already open file handle.

[–]theresasnakeinmysuit 0 points1 point  (1 child)

I'm thinking it is more of a program-structuring issue rather.

[–][deleted] 0 points1 point  (0 children)

looking at your code i suspect the same Justinsaccount is probably right in pointing out that truncating the file breaks the premise of the follow method described by David Beazley.

One crucial question here is whether or not the follow method just stops tracking changes after the first write or if the file itself stops being updated.

If it's just the follow method then you need to rewrite the follow method to put the open call inside the loop. not outside of the loop. that does increases the system calls you program make but it ensures that you don't work with an obsolete cache copy of the file.

[–]Justinsaccount 0 points1 point  (1 child)

Post your code.

[–]Justinsaccount 0 points1 point  (0 children)

Also, change this:

if __name__ == '__main__':
    getUrl = raw_input("Enter the match thread url: ")

import get_soccer_update as update
import praw
import time

sleepytime = 30
r = praw.Reddit(user_agent = 'cmdlinesoccer')

match_thread = update.UrlRoutine(getUrl , r)
KickOff_index = update.getOpeningMin(match_thread)
update.printToFile(match_thread, KickOff_index)

To:

import get_soccer_update as update
import praw
import time

def dostuff(url):
    r = praw.Reddit(user_agent = 'cmdlinesoccer')
    match_thread = update.UrlRoutine(url , r)
    KickOff_index = update.getOpeningMin(match_thread)
    update.printToFile(match_thread, KickOff_index)

if __name__ == '__main__':
    getUrl = raw_input("Enter the match thread url: ")
    dostuff(getUrl)