Parallel Python concurrent development of multi-core parallel programs (Example 2)

Source: Internet
Author: User
#  -*-Coding: UTF-8 -*-  #  -------------------------------------------------------------------------------  #  Name:  #  Purpose:  ##  Author: ankier  ##  Created: 05-02-2013  #  Copyright: (c) ankier 2013  # Licence: <your licence>  #  -------------------------------------------------------------------------------  Import  Math, sys, MD5, time  Import  PP  Def  Md5test (hash, start, end ):  For X In  Xrange (START, end ):  If Md5.new (STR (x). hexdigest () =Hash:  Return  Xppservers = () Job_server = Pp. Server (ppservers = Ppservers)  Print   '  Number of core work processes  ' , Job_server.get_ncpus (), "  Workers  "  Hash = Md5.new ( "  1829182 "  ). Hexdigest ()  Print   "  Hash =  "  , Hashstart_time = Time. Time () rr = Md5test (hash, 1, 2000000 )  Print   '  Time consumed by a single thread  ' , Time. Time ()-start_time, '  S  ' , '  Result =  '  , Rrstart_time = Time. Time () Start = 1 End = 2000000 Parts = 128 Step = (End-Start)/parts + 1 Jobs = []  For Index In  Xrange (parts): starti = Start + Index *Step endi = Min (start + (index + 1 )* Step, end) jobs. append (job_server.submit (md5test, (hash, starti, endi ),(),(  "  MD5  "  ,)))  For Job In  Jobs: Result = Job ()  If  Result:  Break  # Print the results  If  Result:  Print   "  Reverse MD5  " , Hash, "  Is  "  , Result  Else  :  Print   "  Reverse MD5  " , Hash, "  Has not been found  "  Print   "  Time elapsed:  " , Time. Time ()-start_time, "  S  "  Job_server.print_stats () 

Running result:

Current core work process count 4Workershash=48ae4d2d384dcc470e471f4169daa96a time consumed by a single thread3.34699988365 s result = 1829182Reverse MD5For48ae4d2d384dcc470e471f4169daa96aIs1829182Time elapsed:1.55399990082Sjob execution statistics: job count| % Of all jobs | job time sum | time per job |Job Server122 | 100.00/5.4470 | 0.044648 |Localtime elapsed since server Creation4.901999950413 active tasks, 4 Cores

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.