Retake course day32 (process of network programming six III)

Source: Internet
Author: User

What's called horizontal scaling: Increasing the number of computers without improving the performance of your computer

What do you mean open Source: opensource

What's called Virtualization: Running multiple systems at the same time

One joinablequeue module

Joinablequeue Module Description: Two more functions than queue, one is Task_done, the other is join, is dedicated to the global programming, most for the producer and consumer.

Task_done: It's used behind get, telling the OS that it's done with the content.

Join: It says the production data in the queue has been completely processed.

Here's how to use it:

# import multiprocessing# Import time# import random# def sheng (name,q,wuping): # for I in range (5): # Time.slee P (Random.random ()) # ret= '%s%s '% (wuping,i) # q.put (ret) # print (' chef%s created%s '% (Name,ret)) # Q.join () # def Xiao (name,q): # while true:# Time.sleep (Random.random ()) # ret=q.get () # print ('%s ate%s '% (NA Me,ret) # Q.task_done () # if __name__== ' __main__ ': # q=multiprocessing. Joinablequeue () # s1=multiprocessing. Process (target=sheng,args= (' Egon1 ', q, ' Bun ')) # s2=multiprocessing. Process (target=sheng,args= (' Egon2 ', Q, ' Rou ')) # s3=multiprocessing. Process (target=sheng,args= (' Egon3 ', q, ' Bones ') # x1=multiprocessing. Process (target=xiao,args= (' alex1 ', q)) # x2=multiprocessing. Process (target=xiao,args= (' alex2 ', q)) # x1.daemon=true# x2.daemon=true# S1.start () # S2.start () # S3.star T () # X1.start () # X2.start () # S1.join () # S2.join () # S3.join ()

Two Manager: Shared memory space

Dict: Shared with a spoof dictionary that can be shared by multiple

The following 1:

# import multiprocessing# def walk (d): #     d[' count ']-=1# if __name__== ' __main__ ': #     m=multiprocessing. Manager () #     d=m.dict ({' count ': ') # for     I in range: #         w=multiprocessing. Process (target=walk,args= (d,)) #         W.start () #     W.join () #     print (d)

The following 2:

# import multiprocessing# def walk (D,loak): # with     loak:#         d[' count ']-=1# if __name__== ' __main__ ': #     m= Multiprocessing. Manager () #     d=m.dict ({' Count ': +}) #     lock=multiprocessing. Lock () #     li=[]# for     i in range: #         w=multiprocessing. Process (target=walk,args= (D,lock)) #         Li.append (w) #         W.start () # for     J in li:#         J.join () #     Print (j) #     print (d)

Three-process Pool

What is a process pool: creating a certain number of processes

Synchronous and asynchronous: Two ways to submit a task.

Pool: The number of process pools and control processes created, the default number is based on the number of cores of the CPU

Apply: Pass in two parameters, the first one is the specified task. Submits a task to the process pool, implementing serial and synchronous calls. After the task is finished, you will get the result immediately.

The number of open processes is several, and there are several PID.

What is a synchronous call: Commit a task and wait until the end of the task to perform the next task.

# import multiprocessing# Import time# import random# import os# def walk (N): #     print ('%s is walking '%os.getpid ()) #
   
    time.sleep (Random.random ()) #     return n## if __name__== ' __main__ ': #     p=multiprocessing. Pool (4) # for     I in range: #         q=p.apply (walk,args= (i)) #         print (q)
   

Apply_async: Submit a task to the process pool, after the task has been submitted, just submit the task, cannot directly perform the task. Implements a concurrent and bad asynchronous call

Execution method: Close first: Shut down the task so that the task ends

Then join: Wait for a process pool not to submit the task, and the task ends and counts the number of tasks

Last get: Get return value

What is an asynchronous call: After a task is committed, it does not wait in place, but continues to submit the next task. Wait for all tasks to be completed after getting the task with get

As follows:

# import multiprocessing# Import time# import random# import os# def walk (N): #     print ('%s is walking '%os.getpid ()) #
   
    time.sleep (Random.random ()) #     return n## if __name__== ' __main__ ': #     p=multiprocessing. Pool (4) # li=[]# for me in     range: #         Q=p.apply_async (walk,args= (i,)) #         Li.append (q) #     P.close () #     P.join () # for     i in li:#         print (I.get ()) #
   

Four back-tone function

What is a callback function: a function called by a function memory, if the address of the function is passed as a parameter to another function, when the address of the function is used to invoke the function that it only wants, the function that this point refers to is the callback function. Details visit:http://www.cnblogs.com/hainan-zhang/p/6222552.html

Callback: The callback function is added later

The process of the callback function is actually the main process.

Using a callback function to implement a web crawler, need to use the requests module

Get: Get URL

Status_code: Return Status code

Text: View the contents of the download URL

 

from multiprocessing import pool,processimport requestsimport osimport time, Randomdef get (URL): Print ('%s get%s '% (Os.getpid (), URL)) response=requests.get (URL) time.sleep (Random.randint (1, 3)) If Response.status_code = = 200:print ('%s done%s '% (Os.getpid (), URL)) return {' url ': url, ' text ': res Ponse.text}def Parse (DIC): Print ('%s parse%s '% (Os.getpid (), dic[' url ')) time.sleep (1) res= '%s:%s\n '% (dic[' url ') ],len (dic[' text '))) with open (' Db.txt ', ' a ') as F:f.write (res) if __name__ = = ' __main__ ': urls=[' https         ://www.baidu.com ', ' https://www.python.org ', ' https://www.openstack.org ', ' https://help.github.com/', ' http://www.sina.com.cn/' P=pool (2) Start_time=time.time () objs=[] for URLs in Urls:obj=p. Apply_async (get,args= (URL,), callback=parse) #主进程负责干回调函数的活 objs.append (obj) p.close () P.join () print (' Master ', (t Ime.time ()-start_time)) 

  

Retake course day32 (process of network programming six III)

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.