Python 3.x Python操作系统轮询选择

Python 3.x Python操作系统轮询选择,python-3.x,concurrency,Python 3.x,Concurrency,下面是David Beazley的演讲,我的目标是了解Python并发性的基础知识 我想知道为什么pythonselect失败了,它用于轮询操作系统操作系统并检查是否有一些任务需要完成 from socket import * from select import select from collections import deque tasks = deque() recv_wait = {} send_wait = {} def fib(n): if n <= 2:

下面是David Beazley的演讲,我的目标是了解Python并发性的基础知识

我想知道为什么pythonselect失败了,它用于轮询操作系统操作系统并检查是否有一些任务需要完成

from socket import *
from select import select
from collections import deque

tasks = deque()
recv_wait = {}
send_wait = {}

def fib(n):
    if n <= 2:
        return 1
    else:
        return fib(n-1)+fib(n-2)


def run():
    while any([tasks, recv_wait, send_wait]):
        while not tasks:
            can_recv, can_send, _ = select(recv_wait, send_wait, [])
            for s in can_recv:
                tasks.append(recv_wait.pop(s))
            for s in can_send:
                tasks.append(send_wait.pop(s))
        task = tasks.popleft()
        try:
            why, what = next(task)
            if why == 'recv':
                recv_wait[what] = task
            elif why == 'send':
                send_wait[what] = task
            else:
                raise RuntimeError("Arg!!")
        except StopIteration:
            print("task done")
def fib_server(address):
    sock = socket(AF_INET, SOCK_STREAM)
    sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
    sock.bind(address)
    sock.listen(5)
    while True:
        yield 'recv', sock
        client, addr = sock.accept()
        print("Connection", addr)
        tasks.append(fib_handler(client))

def fib_handler(client):
    while True:
        yield 'recv', client
        req = client.recv(100)
        if not req:
            break
        n = int(req)
        result = fib(n)

        result = fib(n)
        resp = str(result).encode('ascii') + b'\n'
        yield 'send',resp
        client.send(resp)
    print("Closed")

tasks.append(fib_server(('', 25000)))
run()

# Separate terminal window
nc localhost 25000
12

# Running Python server

➜  python3 -i aserver.py
Connection ('127.0.0.1', 61098)
Traceback (most recent call last):
  File "aserver.py", line 63, in <module>
    run()
  File "aserver.py", line 20, in run
    can_recv, can_send, _ = select(recv_wait, send_wait,[])
TypeError: argument must be an int, or have a fileno() method.
def fib_handlerclient: 尽管如此: 收益率“recv”,客户 req=client.recv100 如果不需要: 打破 n=intreq 结果=fibn 结果=fibn重复 resp=strresult.encode'ascii'+b'\n' 产生“发送”,resp应产生fd客户端 client.sendrep 打印关闭 def fib_handlerclient: 尽管如此: 收益率“recv”,客户 req=client.recv100 如果不需要: 打破 n=intreq 结果=fibn 结果=fibn重复 resp=strresult.encode'ascii'+b'\n' 产生“发送”,resp应产生fd客户端 client.sendrep 打印关闭
解释是正确的。我相信OP并不意味着产生resp,正如其他收益率报表所示。解释是正确的。我相信OP并不意味着产生resp,正如其他收益率报表所示。