0

Hey I have a question regarding the use of the python module for multiprocessing and its interaction with variables

In essence, my code is having an issue updating a certain variable -- even though I can see it has been updated (?)

The code itself is relatively straight forward -- I want to look at any new lines added to a file (will be a log) and then write each line into a data struct.

My problem is that for some reason my log_array list is still empty at the end of this, even though I have printed it in the generate_new_log function and I can see it being populated.

temp_log = open("temp_log.txt", 'w')
log_array = []

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

def generate_new_log():
    logfile = open("test2.txt","r")
    loglines = follow(logfile)
    for line in loglines:
        #global log_array
        global log_array 
        log_array.append(line)
        print log_array

def main():

    p = multiprocessing.Process(target=generate_new_log, name='NewLog', args = ())
    p.start()
    time.sleep(10)
    print log_array
    p.terminate()
    print("hi")
    p.join()
    print log_array


if __name__ == '__main__':
    main()

The console output looks something like this:

['duck\n']
['duck\n', 's\n']
['duck\n', 's\n', '\n']
['duck\n', 's\n', '\n', 'bbbb\n']
['duck\n', 's\n', '\n', 'bbbb\n', 'cccc\n']
['duck\n', 's\n', '\n', 'bbbb\n', 'cccc\n', 'cerap\n']
['duck\n', 's\n', '\n', 'bbbb\n', 'cccc\n', 'cerap\n', 'nerds\n']
['duck\n', 's\n', '\n', 'bbbb\n', 'cccc\n', 'cerap\n', 'nerds\n', 'duck\n']
['duck\n', 's\n', '\n', 'bbbb\n', 'cccc\n', 'cerap\n', 'nerds\n', 'duck\n',
'pony\n']
[]
hi
[]

The question is mostly that those two empty lists at the bottom of the console refer to the log_array that is like global variable. I can see as we go through the generate_new_log function, I can see log_array being updated. My primary question is -- how/where can I extract the information from the log_array that was populated after that 10 second interval?

  • That's not multithreading. You're using the *multiprocessing* module, which works completely differently, in ways that the documentation doesn't do enough to explain. – user2357112 Jan 18 '17 at 23:01
  • My bad, meant to say multiprocessing -- have been trying to find a solution and mixed the terminology up. – Austin Mei Jan 18 '17 at 23:02
  • Do you have any idea for a workaround or as to why the variable was not being updated? – Austin Mei Jan 18 '17 at 23:03

0 Answers0