Multithreading in python Learning (2); multithreading in python Learning

Source: Internet
Author: User

Multithreading in python Learning (2); multithreading in python Learning

UseMultiprocessing

UseMultiprocessing. dummy

Single UseThe multiprocessing module refers to a multi-process.Multiprocessing. dummyIndicates that multithreading is used.

 

# From multiprocessing import Poolfrom multiprocessing. dummy import Pool as ThreadPool
From multiprocessing import cpu_countimport timeimport urllib2urls = ['HTTP: // www.baidu.com ', 'HTTP: // home.baidu.com/', 'HTTP: // tieba.baidu.com/', 'HTTP: // zhidao.baidu.com/', 'HTTP: // music.baidu.com/', 'HTTP: // image.baidu.com/', 'HTTP: // python-china.org/', 'HTTP: // response ', 'http: // python-china.org/node/', 'HTTP: // python-china.org/account/signin', 'HTTP: // timeout, 'HTTP: // www.qq.com ', 'HTTP: // www.youku.com', 'HTTP: // www.tudou.com '] start = time. time () results = map (urllib2.urlopen, urls) print 'normal: ', time. time ()-startstart2 = time. time () # enable cpu_count worker. If no parameter is set, the cpu core pool = ThreadPool (processes = cpu_count) is used by default. # Run urllib2.urlopen (url) in the thread) the execution result results2 = pool is returned. map (urllib2.urlopen, urls) pool. close () pool. join () print 'thread Pool: ', time. time ()-start2

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.