Python Parallel run function

Source: Internet
Author: User

# -*- coding: utf-8 -*-import multiprocessingimport os, time,randomimport  pymysqlcurdir = os.path.dirname (__file__) def db_conn ():    conn  = pymysql.connect (host= ' localhost ', user= ' root ', password= ' 123456 ', db= ' entries ',  charset= ' UTF8 ')       #conn  = pymysql.connect (host= ' localhost ', user= ' root ', password= ' root ', db= ' db ',  charset= ' UTF8 ',  cursorclass=pymysql.cursors.dictcursor)     return conndef  db_query (Conn, sql):     cursor = conn.cursor ()      cursor.execute (SQL)     result = cursor.fetchall ()      Return resultdef fun_1 ():     fp = open (Os.path.join (curdir,  ' Engines.txt '),  ' W ')     conn = db_conn ()     sql =   ' Select engine,support,comment from engines; '     result = db_query (Conn, sql)     for res  In result:        fp.write ("%s|%s|%s\n"  % (Res[0],res[1],  RES[2])         fp.flush ()     fp.close ()     conn.close () def fun_2 ():     fp = open (Os.path.join ( curdir,  ' collations.txt '),  ' W ')     conn = db_conn ()      sql =  ' Select collation_name,character_set_name,id,is_default, is_compiled from  collations; '     result = db_query (Conn, sql)     for res  In result:        fp.write ("%s|%s|%s|%s|%s\n"  % (res[0],res[1 ], str (res[2]), res[3],res[4])         fp.flush ()     fp.close ()     conn.close () def fun_3 ():     fp = open (Os.path.join (curdir,  ' indexes.txt '),  ' W ')      conn = db_conn ()     sql =  ' Select name,table_id,type,n_ fields,page_no from indexes; '     result = db_query (Conn, sql)     for res  In result:        fp.write ("%s|%s|%s|%s|%s\n"  % (res[0],res[1 ], RES[2],RES[3],RES[4])         fp.flush ()      fp.close ()     conn.close () Def main ():    conn =  Db_conn ()     fun_list = [ fun_1, fun_2, fun_3 ]     print ("parent process %s"  %  os.getpid ())     pool = multiprocessing. Pool (3)     start = time.time ()     for func in  fun_list:        print ("Func name",  func)          pool.apply_async (func)     print (' Waiting for  all subprocess done ... ')     pool.close ()     pool.join ( )     end = time.time ()     print (' All subprocess  Done, run %0.2f seconds '  %  (end - start))     conn.close () if __name__ ==  ' __main__ ':     main ()   # 0.3  -  0.42     "    start = time.time ()      conn = db_conn ()     fun_1 (conn)     fun_2 (cOnn)     fun_3 (conn)     conn.close ()     end =  time.time ()     print (' All done, run %0.2f seconds '  %  ( End - start))    #6 .61 6.94 7.03 7.30 6.91     "

When Fun_1, Fun_2, fun_3 itself is not very time consuming, parallel efficiency is not executed efficiently in sequence.

This article is from the "lang8027" blog, make sure to keep this source http://lang8027.blog.51cto.com/9606148/1794737

Python Parallel run function

Related Article

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.