休眠线程会阻塞 stdin

发布于 2024-08-31 17:00:45 字数 581 浏览 3 评论 0原文

我正在运行一个函数,该函数评估使用 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;

--谢谢

I'm running a function which evaluates commands passed in using stdin and another function which runs a bunch of jobs. I need to make the latter function sleep at regular intervals but that seems to be blocking the stdin. Any advice on how to resolve this would be appreciated.

The source code for the functions is

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;

--Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

长梦不多时 2024-09-07 17:00:45

使用单线程:

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

Use a single thread:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文