1

我想用 Python 编写一个每 30 秒触发一次 select() 的函数。

到目前为止,我的代码如下所示 -

inputs = [ UDPSock , sys.stdin]
outputs = []
while inputs:
  readable, writable, exceptional = select.select(inputs, outputs, inputs)
  for s in readable:
    if s is UDPSock
      # Deal with socket

    elif s is sys.stdin:
      # Deal with input

我想实现以下目标 -

inputs = [ UDPSock , sys.stdin, timer]
outputs = []
while inputs:
  readable, writable, exceptional = select.select(inputs, outputs, inputs)
  for s in readable:
    if s is UDPSock
      # Deal with socket

    elif s is sys.stdin:
      # Deal with input

    elif s is timer:
      # Deal with  timer

理想情况下,如果可能的话,我希望不使用线程。

4

1 回答 1

4

使用可选timeout参数选择有问题吗?

例如

while True:
    ready = readable, writable, exceptional = select.select(inputs, outputs,
                                                            inputs, 30.0)
        if not any(ready):
            #timeout condition
        else:
            #iterate over the ready lists as appropriate
于 2012-12-17T20:27:57.120 回答