python多進程編程

來源:互聯網
上載者:User

標籤:python 入門

最近開始學習PYTHON程式設計語言,詳細參照《python絕技運用Python成為頂級駭客》。在學習過程第一章節中,編寫破解LINUX shadow檔案時,想利用多線程加快破解速度。主機運行環境為WINDOWS下的VM WORKSTATION上的一台虛擬機器,運行多線程代碼後並無任何速度上的提升,並且經常伴隨輸出混亂,不知所以然。故還是利用多進程編寫了一個簡單的指令檔,代碼如下:

import crypt
import multiprocessing
import time
import sys
def check(passwd):
        cryptWord=crypt.crypt(passwd.strip(‘\n‘),salt.strip(‘\n‘))
        if cryptWord==linuxWord:
                print "password is : "+passwd
                print "harked,time now is :"+currtime()
        else:
                pass

def worker(q):
        while not q.empty():
                passwd =q.get()
                try:
                        check(passwd)
                finally:
                        q.task_done()

def currtime():
        return time.ctime()

def txtToArray(DesFilePath):
        desDic=open(DesFilePath,‘r‘)
        passwdlist=[]
        for line in desDic.readlines():
                passwdlist.append(line)
        return passwdlist

if __name__==‘__main__‘:
        global salt,linuxWord
        print "start time is :"+currtime()
        q=multiprocessing.JoinableQueue()
        linuxWord="$6$9CfIODlW$c34lcX4eYVCD9Kyer3kQLcM6o8Q7uryUroT/K0WVSnnqxtGbPJGDRGymJy4gVMnYgLUjc1J5wVEkrm/jxsQ.i"
        salt="$6$9CfIODl$"
        DesFilePath="/script/1/1.15/password.txt"
        passwdlist=txtToArray(DesFilePath)
        map(q.put,passwdlist)
        jobs=[multiprocessing.Process(target=worker,args=(q,)) for i in xrange(10)]
        map(lambda x:x.start(),jobs)
        q.join()
        print "end time is :"+currtime()


運行效率比多線程要快很多

python多進程編程

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.