简体   繁体   中英

What happens in python when you use the thread function in this code

My question is how does python create the thread for the testing of multiple passwords? And why is it more efficient?

import zipfile
from threading import Thread

def extractFile(zFile, password):
    try:
        zFile.extractall(pwd=password)
        print '[+] Found password ' + password + '\n'
    except:
        pass

def main():
    zFile = zipfile.ZipFile('evil.zip')
    passFile = open('dictionary.txt')
    for line in passFile.readlines():
        password = line.strip('\n')
        t = Thread(target=extractFile, args=(zFile, password))
        t.start()

if __name__ == '__main__':
    main()`

The use of multi-threading makes this code less efficient.

The work that is being down here is CPU oriented. Python threads do not work well at distributing CPU oriented work because only one thread can do work at a time (because of the Python GIL or Global Intepreter Lock)!

Python threads are more useful for blocking operations such as network calls, disk reads etc.

On top of this, making a lot of threads adds a great deal of overhead, because there is a thread scheduler constantly switching which thread is running so that no thread starves. In your example, there is a thread being created for each file in the line , which is pretty bad if there is more than 30-50 lines.

To answer your question:

how does python create the thread for the testing of multiple passwords?

It simply runs the function extractFile in a new thread for each line in your file.

and why is it more efficient?

Due to limitations of the Python GIL, and overhead of having more than 20 threads, this is definitely not more efficient than running through the entire file without multi-threading.

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