[Windward Rain] Process + Ctrip

Source: Internet
Author: User
Tags readline

#-*-coding=utf-8-*-ImportRequests fromMultiprocessingImportProcessImportgevent fromGeventImportmonkey; Monkey.patch_all ()Importsys Reload (SYS) sys.setdefaultencoding ('UTF8')  defFetch (URL):Try: S=requests. Session () R= S.get (url,timeout=1)#Crawl the page here    exceptexception,e:Printereturn "'     defProcess_start (url_list): Tasks= []       forUrlinchurl_list:tasks.append (Gevent.spawn (Fetch,url)) Gevent.joinall (tasks)#use the coprocessor to perform  defTask_start (Filepath,flag = 100000):#start a process per 10W URLWith open (filepath,'R') as Reader:#reads a URL from a given fileURL =reader.readline (). Strip () Url_list= []#This list is used to hold the co-taski = 0#counter, which records how many URLs are added to the thread queue         whileurl!="': I+ = 1url_list.append (URL)#each time the URL is read out, the URL is added to the queue            ifi = = Flag:#a certain number of URLs starts a process and executesp = Process (target=process_start,args=(Url_list,)) P.start () url_list= []#Resetting the URL queuei = 0#Reset CounterURL =reader.readline (). Strip ()ifUrl_list not[]:#If there is a URL remaining in the task queue after exiting the loopp = Process (target=process_start,args= (url_list,))#Put all the remaining URLs in the last process to executeP.start ()if __name__=='__main__': Task_start ('./testdata.txt')#read the specified file

[Windward Rain] Process + Ctrip

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.