[Python] How can I clear or reduce the amount of memory a Javascript injection is taking up in...

Discussão em 'Python' iniciado por Stack, Julho 16, 2025.

  1. Stack

    Stack Membro Participativo

    I'm currently attempting to access metadata off of an online video stream by running the video player in Selenium in python and attempting to return any data from the metadata parser. This is my current code:

    driver.execute_script("""
    window.__metaDataCaptured__ = [];
    function hookParserOutput(data) {
    window.__metaDataCaptured__.push(data)
    }

    const oldParser = metaDataParser.parse
    metaDataParser.parse = function(msg) {
    result = oldParser(msg)
    hookParserOutput(result)
    }
    """)
    time.sleep(5)

    while True:
    result = driver.execute_script("""
    const data = window.__metaDataCaptured__ || [];
    window.__metaDataCaptured__ = null; // clear after read
    window.__metaDataCaptured__ = []; // clear after read
    return data;
    """)
    if result != []:
    print(result)


    Right now, the website slows down significantly after a couple of minutes and crashes after around 20 minutes.

    Eventually I'd like to push the result data to another structure, but I'm printing for now. I've tried my best to remove any memory leaks by reusing variable names and clearing __metaDataCaptured__, but after just a couple of minutes the Developer Tools show memory usage at hundreds of megabytes, which is primarily taken up by strings, mostly the data I'm trying to intercept.

    I figured that constantly executing a new script is not great for performance. Is there a way I can either

    1. Constantly stream new data to my python code without running a new script each time, or
    2. Clear the data after it's been received so it doesn't take up so much memory?

    Continue reading...

Compartilhe esta Página