用于无限数据输入(流)的python线程和队列

用于无限数据输入(流)的python线程和队列,python,multithreading,queue,multiprocessing,Python,Multithreading,Queue,Multiprocessing,我想使用线程来处理流式输入 例如,如何使用itertools.count生成无限输入的以下代码 以下代码在以下情况下有效: 'for i in itertools.count():'替换为'for i in xrange(5):' 也许我遗漏了一些东西,但这难道不像在for循环之前创建和启动线程那么简单吗 另外,让线程在没有工作时终止似乎是个坏主意,因为将来可能会有更多的工作出现。你肯定想让它们阻塞直到有工作可用吗?问题是itertools.count生成一个无限序列。这意味着for循环永远不会

我想使用线程来处理流式输入

例如,如何使用itertools.count生成无限输入的以下代码

以下代码在以下情况下有效: 'for i in itertools.count():'替换为'for i in xrange(5):'


也许我遗漏了一些东西,但这难道不像在
for
循环之前创建和启动线程那么简单吗


另外,让线程在没有工作时终止似乎是个坏主意,因为将来可能会有更多的工作出现。你肯定想让它们阻塞直到有工作可用吗?

问题是
itertools.count
生成一个无限序列。这意味着for循环永远不会结束。你应该把它放在它自己的函数中,让它成为一个单独的线程。这样,当工作线程从队列中获取数据时,队列将不断增长。

您需要用线程填充队列。您需要管理队列大小。尤其是如果工人花时间处理物品。您需要标记队列项目已完成。如果这与您关于twitter和“极快”输入的其他问题有关,那么您在数据库插入方面还有很多工作要做

你的问题在相当复杂的话题上太模糊了。你似乎对自己想要达到的目标了解得不够,以至于知道这并不容易。我建议你在你想做的事情上更具体一点

下面是一个用线程填充和使用队列的示例。队列大小未被管理

from threading import Thread
from Queue import Queue, Empty, Full
import itertools
from time import sleep


def do_work(q,wkr):
  while True:
    try:
      x = q.get(block=True,timeout=10)
      q.task_done()
      print "Wkr %s: Consuming %s" % (wkr,x)
      sleep(0.01)
    except Empty:
      print "Wkr %s exiting, timeout/empty" % (wkr)
      break
    sleep(0.01)

def fill_queue(q,limit=1000):
  count = itertools.count()
  while True:
    n = count.next()
    try:
      q.put(n,block=True,timeout=10)
    except Full:
      print "Filler exiting, timeout/full"
      break
    if n >= limit:
      print "Filler exiting, reached limit - %s" % limit
      break
    sleep(0.01)

work_queue = Queue()

threads = [Thread(target=do_work, args=(work_queue,i)) for i in range(2)]
threads.insert(0,Thread(target=fill_queue,args=(work_queue,100)))

for t in threads:
  t.start()

for t in threads:
  t.join()

 Wkr 0: Consuming 0
 Wkr 1: Consuming 1
 Wkr 0: Consuming 2
 Wkr 1: Consuming 3
 ....
 Wkr 1: Consuming 99
 Filler exiting, reached limit - 100
 Wkr 0: Consuming 100
 Wkr 1 exiting, timeout/empty
 Wkr 0 exiting, timeout/empty

关于
工作
中的
中断
,您提出了一个很好的观点。除非队列缓冲了足够多的数据,否则工作线程可能会在更多信息放入队列之前全部终止。@Joey这听起来像是一个家庭作业问题。如果是这样,请将其标记为这样。关于无限循环,您是对的。我也这么想。但是,当我将itertools放在自己的线程中时,它会给我一个运行时错误。请运行一些代码。运行时错误VisualC++。这个应用程序请求运行时以异常的方式终止它。如何使下面的代码做什么?
from threading import Thread
from Queue import Queue, Empty, Full
import itertools
from time import sleep


def do_work(q,wkr):
  while True:
    try:
      x = q.get(block=True,timeout=10)
      q.task_done()
      print "Wkr %s: Consuming %s" % (wkr,x)
      sleep(0.01)
    except Empty:
      print "Wkr %s exiting, timeout/empty" % (wkr)
      break
    sleep(0.01)

def fill_queue(q,limit=1000):
  count = itertools.count()
  while True:
    n = count.next()
    try:
      q.put(n,block=True,timeout=10)
    except Full:
      print "Filler exiting, timeout/full"
      break
    if n >= limit:
      print "Filler exiting, reached limit - %s" % limit
      break
    sleep(0.01)

work_queue = Queue()

threads = [Thread(target=do_work, args=(work_queue,i)) for i in range(2)]
threads.insert(0,Thread(target=fill_queue,args=(work_queue,100)))

for t in threads:
  t.start()

for t in threads:
  t.join()

 Wkr 0: Consuming 0
 Wkr 1: Consuming 1
 Wkr 0: Consuming 2
 Wkr 1: Consuming 3
 ....
 Wkr 1: Consuming 99
 Filler exiting, reached limit - 100
 Wkr 0: Consuming 100
 Wkr 1 exiting, timeout/empty
 Wkr 0 exiting, timeout/empty