2012-12-17 64 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