python多程序程式設計

2021-12-29 20:04:09 字數 1510 閱讀 3751

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/k0wvsnnqxtgbpjgdrgymjy4**mnyglujc1j5wvekrm/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多程序 Python多程序程式設計詳解

本文 在 python 3.6 環境下測試通過。多程序 multiprocessing 模組是在 python 2.6 版本中加入的,和多執行緒 threading 模組類似,都是用來做並行運算的。不過python既然有了threading,為什麼還要搞乙個multiprocessing呢?這是因為...

python 多程序程式設計

多程序指的是乙個程式可以啟動多個程序執行,一般模式如下 import multiprocessing from multiprocessing import process,current process import time cup 核數量 num cpus multiprocessing.cp...

Python 多程序程式設計

import multiprocessing import time import osimport random g nums 11,22,33 deftest1 while true print 子程序pid d,父程序pid d os.getpid os.getppid time.sleep ...