Day 39 process and thread return value basis

1. In the process pool map function, which is an asynchronous, and comes close and join function

A procedure explained below:

from multiprocessing Import   Pool
 Import Time
 DEF FUNC (n-): 
    the time.sleep ( . 1 )
     return n-* n-
 IF  the __name__ == ' __main__ ' : 
    P = Pool ()
     for I in Range (10 ): 
        RET = p.apply_async (FUNC , args = (i,))
         Print (ret.get ()) # GEI will make this step into the blocked state when execution is completed after the func get results
View Code

Usage 2 callbacks: Sometimes we deal with multiple websites one time, when the process data reptiles, we read and download processes into sub-processes from the site, this time has been in the main process

Wait state (idle state) cpu utilization decreased, this time we can use a callback function to handle the process of obtaining a value for the primary treatment process went.

 

 3. how to install the module requests:

  First open the cmd, switch the disk is re-add command line, type the disk number: enter with a python under the disk directory, and then locate the installation file path pythonpip of (generally under Scripts) If you forget to install the software, you can right position , then open the file location properties, find the installation directory of the file, and then enter cmd command and type cd C: \ python \ Scripts, then use the dir command to view whether the following

 

 Then using a command to install the module pip install requests, if an error occurs, it is necessary to update the pip, to enter python -m pip install --upgrade pip, then automatically enters update state, after the update is complete, the input

pip install requests will be installed successfully

 

4. p'y'charm how to change with the re-use of python version:

Open the file in setting-- then find project-- to find project intrepretation

 

 

5. requests to write programs:

Import Requests 
respone = requests.get ( ' http://www.baidu.com ' )
 Print (respone)
 Print (respone.status_code) # status code printed page 
Print (respone.content) 
result bits
 <Response [200]> 
200 is 
B '<!DOCTYPE html>\r\n<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title>\xe7\x99\xbe\xe5\xba\xa6\xe4\xb8\x80\xe4\xb8\x8b\xef\xbc\x8c\xe4\xbd\xa0\xe5\xb0\xb1\xe7\x9f\xa5\xe9\x81\x93</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class="bg s_ipt_wr"><input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus></span><span class="bg s_btn_wr"><input type=submit id=su value=\xe7\x99\xbe\xe5\xba\xa6\xe4\xb8\x80\xe4\xb8\x8b class="bg s_btn"></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>\xe6\x96\xb0\xe9\x97\xbb</a> <a href=http://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>\xe5\x9c\xb0\xe5\x9b\xbe</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>\xe8\xa7\x86\xe9\xa2\x91</a> <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>\xe8\xb4\xb4\xe5\x90\xa7</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&amp;tpl=mn&amp;u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>\xe7\x99\xbb\xe5\xbd\x95</a> </noscript> <script>document.write(\'<a href="http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u=\'+ encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ \'" name="tj_login" class="lb">\xe7\x99\xbb\xe5\xbd\x95</a>\');</script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style="display: block;">\xe6\x9b\xb4\xe5\xa4\x9a\xe4\xba\xa7\xe5\x93\x81</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>\xe5\x85\xb3\xe4\xba\x8e\xe7\x99\xbe\xe5\xba\xa6</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>&copy;2017&nbsp;Baidu&nbsp;<a href=http://www.baidu.com/duty/>\xe4\xbd\xbf\xe7\x94\xa8\xe7\x99\xbe\xe5\xba\xa6\xe5\x89\x8d\xe5\xbf\x85\xe8\xaf\xbb</a>&nbsp; <a href=http://jianyi.baidu.com/ class=cp-feedback>\xe6\x84\x8f\xe8\xa7\x81\xe5\x8f\x8d\xe9\xa6\x88</a>&nbsp;\xe4\xba\xacICP\xe8\xaf\x81030173\xe5\x8f\xb7&nbsp; <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>\r\n'
View Code

From the results, the status of this page 200, and page 200 is a normal state, if the state value is 404 or 502, it indicates that the page fault, landing is not improved.

import requests
from multiprocessing import  Pool
def get(url):
    respone=requests.get(url)
    if respone.status_code==200:
        return url,respone.status_code
def print_url(m):
    print('%s\t%s'%(m[0],m[1]))
if __name__=='__main__':
    p=Pool()
    url_list=[
        'https://www.baidu.com',
        'https://www.sohu.com/'
        'https://www.sogou.com/'
        'https://pinyin.sogou.com/'
    ]
    for url in url_list:
        p.apply_async(get,args=(url,),callback=print_url)
    p.close()
    p.join()
结果为
https://www.baidu.com    200
https://www.sohu.com/https://www.sogou.com/https://pinyin.sogou.com/    200
View Code

6. reptiles previous program review:

 

Guess you like

Origin www.cnblogs.com/ab461087603/p/12502412.html