码迷,mamicode.com
首页 > 其他好文 > 详细

用POLL的方式,没有跑出结果来,立此存照

时间:2015-09-15 17:59:35      阅读:131      评论:0      收藏:0      [点我收藏+]

标签:

咦,这些内容,和我以前看内核时的东东,对应起来了。。

SELECT,POLL,EPOLL,非阻塞,异步之类的。。。

但我没有调出来。回家有空了可以看看,不用再敲打代码啦。。。

#!/usr/bin/env python
import socket, sys, select

port = 54321
host = 127.0.0.1

spinsize = 10
spinpos = 0
spindir = 1

def spin():
    global spinsize, spinpos, spindir
    spinstr = . * spinpos +     | + . * (spinsize - spinpos -1 )
    sys.stdout.write(\r + spinstr +  )
    sys.stdout.flush()

    spinpos += spindir
    if spinpos < 0:
    spindir = 1
    spinpos = 1
    elif spinpos >= spinsize:
    spinpos = 2
    spindir = -1

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, port))

p = select.poll()
p.register(s.fileno(), select.POLLIN | select.POLLERR | select.POLLHUP)

while 1:
    results = p.poll(50)
    if len(results):
    if results[0][1] == select.POLLIN:
        data = s.recv(4096)
        if not len(data):
        print "\rRemote end closed connection; exiting."
        break
        sys.stdout.write("\rReceived: " + data)
        sys.stdout.flush()
    else:
        print "\rProblem occurred; exiting."
        sys.exit(0)
spin()

 

用POLL的方式,没有跑出结果来,立此存照

标签:

原文地址:http://www.cnblogs.com/aguncn/p/4810769.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!