前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python多进程编程

python多进程编程

作者头像
py3study
发布2020-01-13 10:11:34
4110
发布2020-01-13 10:11:34
举报
文章被收录于专栏:python3

最近开始学习PYTHON编程语言,详细参照《python绝技运用Python成为顶级***》。在学习过程第一章节中,编写破解LINUX shadow文件时,想利用多线程加快破解速度。主机运行环境为WINDOWS下的VM WORKSTATION上的一台虚拟机,运行多线程代码后并无任何速度上的提升,并且经常伴随输出混乱,不知所以然。故还是利用多进程编写了一个简单的脚本文件,代码如下:

代码语言:javascript
复制
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()

运行效率比多线程要快很多

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/08/09 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档