【问题标题】:coroutine receiving data with a thread calculating throughtput in python协程在python中使用计算吞吐量的线程接收数据
【发布时间】:2015-02-21 00:22:01
【问题描述】:

我希望有一个协程连续接受数据并使其以固定间隔产生速度(例如:就每分钟接收的项目而言)

@coroutine
def speed(target=None):
    messages = Queue()
    def calculate():
        while True:
           time.sleep(1)
           messages.put(1/count)
    Thread(target=calculate).start()
    while True:
        item = (yield)
        count += 1
        ... how do I receive messages in a nonblocking fashion?

我试图弄清楚我是否需​​要启动一个线程并使用一个队列从该队列中产生,或者以其他方式来执行此并行计算(速度、吞吐量、平均值、以固定间隔发出最后一个值、等)

【问题讨论】:

  • 这是真正的 Python 代码吗?
  • 是的,不是完整的程序,而是真实的代码。 coroutine 装饰器来自@dabeaz
  • 好吧,我假设您在第二行缺少 def,而在第 9 行,t 可能应该大写。

标签: python multithreading yield


【解决方案1】:

我用线程解决了。

@coroutine
def speed(interval, target):
    # calculate events per second on every received item
    def counter():
        print "thread started"
        while True:
            time.sleep(interval);
            # send to redis, message queue, local storage, coroutine or whatever you need
            # ...
            print "inside thread. count = %s per %s seconds" % (speed.count, interval)

            if close:
                target.close()
                print "end of thread"
                return

            with lock:
                speed.count=0

    lock = Lock()
    speed.count=0
    close=False

    t=Thread(target=counter)
    t.daemon=True
    t.start()

    try:
        while True:
            line = (yield)           # Receive a line
            with lock :
                speed.count+=1

    except GeneratorExit:
        #signal the counting thread to close
        close = True

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-04
    • 1970-01-01
    • 2011-03-05
    • 2018-02-23
    • 1970-01-01
    • 1970-01-01
    • 2012-07-02
    • 2017-05-07
    相关资源
    最近更新 更多