Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Can a thread be created to keep the queue populated with lines from the source file, text.txt, as well as simultaneously write the results to the output file tagfile.csv when the tagreset value is equal to the size of the tagdict?

tagdict = {}
tagreset = 10

with open('text.txt') as f:
    for line in f:
        if (len(tagdict) == tagreset):
            tagfile = open("tagfile.csv","a")
            for key in tagdict:
                tagstring = ':' + (int(tagdict[key])-1) + '"\n"' + key + '","' + tagdict[key]
                tagfile.write(tagstring)
                tagfile.close()
                tagdict = {}
        q.put(line)

def worker(queue):
    mutex.acquire()
    try:
        queue_full = True
        while queue_full:
        try:
            for match in re.finditer('\<tag\>(.*?)\<\/tag\>',line):
                try:
                    tagdict[match.group(0)] = match.start()
                except:
                    print "no title matches found"
        except Queue.Empty:
            queue_full = False
    finally:
        mutex.release()
thread_count = 5
for i in range(thread_count):
    t = Thread(target=worker, args = (q,))
    t.start()
share|improve this question
    
Why would you use multiple threads with all the required synchronization overhead to implement a simple filter? –  msw Oct 15 '13 at 3:13
    
I'm scanning a large file with a low performance computer –  user1371414 Oct 15 '13 at 4:39
1  
But unless your computer uses an SSD (unlikely if it's "low performance") most of the time would be spent waiting for the hard drive anyway. More threads won't make the disk spin faster. –  Lambda Fairy Oct 15 '13 at 4:58
    
The threads are in use so I can scan multiple pieces of the file simultaneously opposed to scanning one line at a time with the assumption that it would be faster. Am I wrong in making that assumption? –  user1371414 Oct 15 '13 at 21:13

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.