触发select()的函数

时间:2012-12-17 20:23:47

标签: python

我想在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

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

1 个答案:

答案 0 :(得分:4)

select使用可选的timeout参数是否有问题?

e.g。

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