简体   繁体   中英

Python MemoryError with Queue and threading

I'm currently writing a script that reads reddit comments from a large file (5 gigs compressed, ~30 gigs of data being read). My script reads the comments, checks for some text, parses them, and sends them off to a Queue function (running in a seperate thread). No matter what I do, I always get a MemoryError on a specific iteration (number 8162735 if it matters in the slightest). And I can't seem to handle the error, Windows just keeps shutting down python when it hits. Here's my script:

import ujson
from tqdm import tqdm
import bz2
import json
import threading
import spacy
import Queue
import time
nlp = spacy.load('en')
def iter_comments(loc):
    with bz2.BZ2File(loc) as file_:
        for i, line in (enumerate(file_)):
            yield ujson.loads(line)['body']
objects = iter_comments('RC_2015-01.bz2')
q = Queue.Queue()
f = open("reddit_dump.bin", 'wb')
def worker():
    while True:
        item = q.get()
        f.write(item)
        q.task_done()
for i in range(0, 2):
    t = threading.Thread(target=worker)
    t.daemon = True
    t.start()
def finish_parse(comment):
    global q
    try:
        comment_parse = nlp(unicode(comment))
        comment_bytes = comment_parse.to_bytes()
        q.put(comment_bytes)
    except MemoryError:
        print "MemoryError with comment {0}, waiting for Queue to empty".format(comment)
        time.sleep(2)
    except AssertionError:
        print "AssertionError with comment {0}, skipping".format(comment)
for comment in tqdm(objects):
    comment = str(comment.encode('ascii', 'ignore'))
    if ">" in comment:
        c_parse_thread = threading.Thread(target=finish_parse, args=(comment,))
        c_parse_thread.start()          
q.join()
f.close()

Does anybody know what I'm doing wrong?

Looks like its not in your code but may be in the data. Have you tried to skip that iteration?

x = 0
for comment in tqdm(objects):
    x += 1
    if x != 8162735

        comment = str(comment.encode('ascii', 'ignore'))
        if ">" in comment:
            c_parse_thread = threading.Thread(target=finish_parse, args=(comment,))
            c_parse_thread.start()  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM