2012-12-17 60 views
1

我想用Python编写一个函数,每30秒触发一次select()。函数触发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

使用可选timeout参数到select有什么问题吗?

例如

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 
+0

谢谢!这工作完美。 – Fletch

+0

@Fletch,如果这有助于您随时接受我的回答(勾号按钮),以便将来帮助他人引用此问题。 – cmh