睡一个线程阻止stdin

时间:2010-05-11 09:01:38

标签: python multithreading time sleep

我正在运行一个函数来评估使用stdin传递的命令和另一个运行一堆作业的函数。我需要让后一个函数定期休眠,但这似乎阻止了标准输入。如何解决这个问题的任何建议将不胜感激。

函数的源代码是

def runJobs(comps, jobQueue, numRunning, limit, lock):
  while len(jobQueue) >= 0:
      print(len(jobQueue));
      if len(jobQueue) > 0:
          comp, tasks = find_computer(comps, 0);
            #do something
        time.sleep(5);

def manageStdin():
    print "Global Stdin Begins Now"
    for line in fileinput.input():
        try:
            print(eval(line));
        except Exception, e:
            print e;

- 由于

1 个答案:

答案 0 :(得分:1)

使用单个线程:

import time
import select
import logging
import sys

def stdinWait(interval):
    start = time.time()
    while True:
        time_left = interval - (time.time() - start)
        if time_left <= 0:
            break
        r, w, x = select.select([sys.stdin], [], [], time_left)
        if r:
            line = r[0].readline()
            try:
                print(eval(line));
            except Exception, e:
                logging.exception(e)

def runJobs(comps, jobQueue, numRunning, limit, lock):
  while len(jobQueue) >= 0:
      print(len(jobQueue));
      if len(jobQueue) > 0:
          comp, tasks = find_computer(comps, 0);
          #do something
          stdinWait(5) # wait 5 seconds while watching stdin