gpt4 book ai didi

python - Python Circuits 框架中的延迟/挂起+恢复事件(处理)

转载 作者:太空宇宙 更新时间:2023-11-03 17:30:53 25 4
gpt4 key购买 nike

在我的项目中,我使用 Worker进行一些与事件相关的文件处理并避免阻塞其他事件的处理。我需要的是能够在工作人员完成时将事件(需要文件处理)置于待命状态(其他组件不会进一步处理),然后恢复处理序列。

当工作线程完成时,我无法发出新事件,因为这可能会导致其他组件多次重新处理“相同”事件。

有什么方法可以延迟(即暂停和恢复)事件传播来完成我需要的事情吗?有更好的方法来解决我的用例吗?

附加说明:我无法避免阻塞行为,因为我需要使用一些外部(阻塞)库调用。

编辑:源码示例:

from time import sleep
from circuits import Component, Debugger, handler, Event, Worker, task


class my_event(Event):
pass


def heavy_task():
print "heavy task"
sleep(3)


class NextHandler(Component):
@handler("my_event", priority=10)
def my_event(self, event):
print "Handler 2"


class Handler(Component):
_worker = Worker()

@handler("my_event", priority=20)
def my_event(self, event):
self.fire(task(heavy_task), self._worker)
print "Handler 1"
# how do I delay "event" until "heavy_task" is completed?


class App(Component):
h1 = Handler()
h2 = NextHandler()

def started(self, component):
print "Running"
self.fire(my_event())


if __name__ == '__main__':
(App() + Debugger()).run()

在这种情况下,我想延迟“Handler”事件,以便“NextHandler”在“heavy_task”完成工作之前不会收到它。

最佳答案

根据您更新的问题和编辑;这就是您“等待”事件完成的方式:

示例:

from time import sleep
from circuits import Component, Debugger, handler, Event, Worker, task


class my_event(Event):
pass


def heavy_task():
print "heavy task"
sleep(3)


class NextHandler(Component):
@handler("my_event", priority=10)
def my_event(self, event):
print "Handler 2"


class Handler(Component):
_worker = Worker(process=True)

@handler("my_event", priority=20)
def my_event(self, event):
# Fire and Wait for: task()
# This happens asynchornously under the covers
# but gives you a synchronous API. Effectively
# turning my_event into a coroutine.
yield self.call(task(heavy_task), self._worker)

# This will only print after task() is complete.
print "Handler 1"


class App(Component):
h1 = Handler()
h2 = NextHandler()

def started(self, component):
print "Running"
self.fire(my_event())


if __name__ == '__main__':
(App() + Debugger()).run()

输出:

$ python app.py 
<registered[worker] (<Worker/worker 5848:MainThread (queued=0) [S]>, <Handler/* 5848:MainThread (queued=0) [S]> )>
<registered[*] (<Handler/* 5848:MainThread (queued=0) [S]>, <App/* 5848:MainThread (queued=4) [R]> )>
<registered[*] (<NextHandler/* 5848:MainThread (queued=0) [S]>, <App/* 5848:MainThread (queued=3) [R]> )>
<registered[*] (<Debugger/* 5848:MainThread (queued=0) [S]>, <App/* 5848:MainThread (queued=2) [R]> )>
<started[*] (<App/* 5848:MainThread (queued=1) [R]> )>
Running
<my_event[*] ( )>
Handler 2
<task[<Worker/worker 5848:MainThread (queued=0) [S]>] (<function heavy_task at 0x7f359df6ade8> )>
heavy task
<task_done[<Worker/worker 5848:MainThread (queued=0) [S]>] (None )>
<task_success[<Worker/worker 5848:MainThread (queued=0) [S]>] (<task[<Worker/worker 5848:MainThread (queued=0) [S]>] (<function heavy_task at 0x7f359df6ade8> )>, None )>
Handler 1

请参阅上面的评论。

关于python - Python Circuits 框架中的延迟/挂起+恢复事件(处理),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31868274/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com