Python non-blocking

Source: Internet
Author: User

SetTimeout

Setblocking+select (Understanding Select)

Continue yesterday's work, this time I use select+setblocking and settimeout to make a contrast, to prove.

First I set the socket to non-blocking. Then use Select to monitor the socket.

#!/usr/bin/envpython# encoding: utf-8import socketimport threadingimport queueimport timeimport  selectclass worker (Threading. Thread):     def __init__ (Self,name,queue):         self.data=queue        super (worker,self). __init__ (Name=name)     def run (self):         for i in  range (10000):             self.data.put (i) Class customer (Threading. Thread):     def __init__ (Self,name,queue):         super (customer,self). __init__ (Name=name)         self.data=queue     def scan (self,ip,port):         s= Socket.socket (Socket.af_inet,socket. SOCK_STREAM)     &Nbsp;   s.setblocking (False)          #s. settimeout (0.1)         RESULTS=S.CONNECT_EX ((ip,port))           #print  results        a,b,c=select.select ([],[s ,],[s,],0.1) #设置超时时间0.1 seconds, here I summarize according to the previous blog post.          #当connect连接成功的时候, represents the TCP3 handshake, and becomes writable at this time          if b: #如果socket可写, represents the port is open              print port        s.close ()      def run (self):        while true:             try:                 a=self.data.get (1,5)             except:                 break             else:                 ip= ' 220.181.136.241 '                  self.scan (Ip,a) Def main ():     start=time.time ()      Queue=queue.queue ()     pool=[]    worke=worker (' Firebroo ', Queue)      worke.start ()     for i in range (:   )      a=customer (' Firebroo ', queue)          Pool.append (a)     for i in pool:         i.start ()      worke.join ()     for i in pool:         i.join ()     print time.time ()-start    del  pool[:]if __name__== ' __main__ ':     main ()

It takes about 17.5 seconds to execute. Below I use settimeout.

#!/usr/bin/envpython# encoding: utf-8import socketimport threadingimport queueimport timeimport  selectclass worker (Threading. Thread):     def __init__ (Self,name,queue):         self.data=queue        super (worker,self). __init__ (Name=name)     def run (self):         for i in  range (10000):             self.data.put (i) Class customer (Threading. Thread):     def __init__ (Self,name,queue):         super (customer,self). __init__ (Name=name)         self.data=queue     def scan (self,ip,port):         s= Socket.socket (Socket.af_inet,socket. SOCK_STREAM)          #s. setblocking (False)         s.settimeout (0.1) # To be consistent with the front, of course, set 0.1 seconds         results=s.connect_ex ((ip,port))          if results==0: #connect_ex中0代表端口开放, 3-time handshake complete              print port          #print  results         #a, B,c=select.select ([],[s,],[s,],0.1 ) #设置超时时间0.1 seconds, here I summarize according to the previous blog post.          #当connect连接成功的时候, represents the TCP3 handshake, and becomes writable at this time           #if  b: #如果socket可写, which represents the port is open         #     print port        s.close ()      def run (self):         while true:            try:                 a=self.data.get (1,5)              except:                 break             Else:                ip= ' 220.181.136.241 '                  self.scan (Ip,a) Def main ():     start=time.time ()     queue= Queue.queue ()     pool=[]    worke=worker (' Firebroo ', Queue)      worke.start ()     for i in range (:    )     a=customER (' Firebroo ', queue)         pool.append (a)     for  i in pool:        i.start ()      Worke.join ()     for i in pool:         i.join ()     print time.time ()-start    del pool[:]if  __name__== ' __main__ ':     main ()

The time is about 17.4. These two test results should be said to be the same, inevitably there will be errors.

Summary: So I can say that Python's settimeout this API is really non-blocking. And the select+setblocking effect is the same

Python non-blocking

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.