我是ZeroMQ的新手。我想知道是否存在一种设置回调的方法,一旦消息出现,就可以调用该回调,我的意思是一旦流中存在数据。
我不想使用线程和循环来指示数据输入,然后调用回调。
您知道如何解决此问题吗?
答案 0 :(得分:0)
自从v2.x到最近的v4.2 +,ZeroMQ本机API从未使用过与低级内部性相关的回调。所有API服务的设计都旨在响应.poll( 0 )
或.recv( zmq.NOBLOCK )
方法,并让用户应用程序做出相应决定并采取相应行动。
如果确实需要使用有线回调,则有两种可能的转发方式:
1)使用通用的python工具- Tkinter.mainloop()
工厂,在该工厂中,可以使用 StringVar()
设置阻止处理程序实例,由.recv()
这样设置(此处是一种阻止方式): aStringVAR.set( aSocket.recv() )
,在该实例上还关联了一个Tracer,该跟踪器调用了所需的回调处理程序,以响应StringVar获取存储的新值。
>>> #-----------------------------------------------FAST MOCK-UP EXAMPLE
>>> import Tkinter as tk # python27
>>> root = tk.Tk()
>>> root.protocol( "WM_DELETE_WINDOW", root.quit() )
'3071841620Ldestroy'
>>> #------VAR-------------------------------------IMPORTANT TOOL:
>>> aStringVAR = tk.StringVar()
>>> aStringVAR.set( "_init_" )
>>> def aKeyPressEventHANDLER( anEvent ): # SIMPLE EventHANDLER,
# # also ignites remote responsive processes
... aTemplate = "[KEY]::{3: >10s}\n<s/n>::{0: >10d}\n(=@=)::{1: > 10d}\n^from::({5:})"
... sString = aTemplate.format( anEvent.serial,
... anEvent.time,
... anEvent.char,
... anEvent.keysym,
... anEvent.keysym_num,
... str(anEvent.widget )
... )
... aStringVAR.set( sString ) # <----------------- emulated instead of aSocket.recv()
... print sString
...
>>> #----VAR_TRACER----------------------------------------[#1]
>>> def aVAR_TRACER_A( p1_quasiNAME, p2_indexOrEmptyString, p3_accessMODE ):
... print "aVAR_TRACER_A()-called::(on){0:} traced_event({1:})".format( str( p1_quasiNAME ), str( p3_accessMODE ) )
... # ###############=[A]#######
... # < do some task =[A] here >
... # ###############=[A]#######
... print "aVAR_TRACER_A() [{0:}]".format( str( root.globalgetvar( p1_quasiNAME ) ).replace( " ", "" ) )
...
>>> #-----VAR_A_tracer_ID------------------------------"w" EVENT SNIFFER WAITING FOR ASSIGNMENT INTO aStringVAR
>>> aTraceVAR_A_tracer_ID = aStringVAR.trace_variable( "w", aVAR_TRACER_A )
aVAR_TRACER_A()-called::(on)PY_VAR0 traced_event(w)
aVAR_TRACER_A() [[KEY]::a<s/n>::832(=@=)::88486992^from::(.)]
[KEY]:: a
<s/n>:: 832
(=@=):: 88486992
^from::(.)
aVAR_TRACER_A()-called::(on)PY_VAR0 traced_event(w)
aVAR_TRACER_A() [[KEY]::KP_6<s/n>::832(=@=)::88509107^from::(.)]
[KEY]:: KP_6
<s/n>:: 832
(=@=):: 88509107
^from::(.)
aVAR_TRACER_A()-called::(on)PY_VAR0 traced_event(w)
aVAR_TRACER_A() [[KEY]::KP_Multiply<s/n>::832(=@=)::88541180^from::(.)]
[KEY]::KP_Multiply
<s/n>:: 832
(=@=):: 88541180
^from::(.)
或更复杂,如果使用直接注入的Tkinter
虚拟事件:
################################################ SETUP EVENT-ROUTING Injector
self.aSigFromZMQ = "<<aVirtualEventSignalledFromZMQ_LAYER>>"
self.bind( self.aSigFromZMQ, anEventHANDLER )
# |
# .bind <<virtual_EventNAME>> altogether with <anEventHANDLER>-call
################################################ Context-fully TRIGGER Injector
self.event_generate( self.aSigFromZMQ, aSigContextDICT )
# |
# .event_generate( <eventNameId>, **args ) # triggers <eventNameId>
# # + passes **args, that allows
# # to set <keyword>=<value> pairs for Event-fields,
# # that are passed to anEventHANDLER via <Event>-object .
2),分别更改ZeroMQ并使用nanomsg。 NNG新API,可以使回调机制与套接字状态/事件相关联。