Class: ZMQ::PollInterruptible

Inherits:
Poll
  • Object
show all
Defined in:
lib/0mq/poll_interruptible.rb

Overview

An interruptible version of Poll.

Instance Attribute Summary

Attributes inherited from Poll

#timeout

Instance Method Summary collapse

Methods inherited from Poll

poll, poll_nonblock, #run_nonblock

Constructor Details

#initialize(*sockets) ⇒ PollInterruptible

Creates the additional interruption objects and calls super Note that either #kill or #close MUST be called when done with the object. There is no automatic finalizer for this object.



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/0mq/poll_interruptible.rb', line 10

def initialize(*sockets)
  @int_context = ZMQ::Context.new
  @int_sock_rep = ZMQ::Socket.new ZMQ::REP, context:@int_context
  @int_sock_req = ZMQ::Socket.new ZMQ::REQ, context:@int_context
  @int_sock_rep.bind    "inproc://int"
  @int_sock_req.connect "inproc://int"
  
  @dead = false
  
  super @int_sock_rep, *sockets
end

Instance Method Details

#closeObject

Permanently kill the Poll object This should be run once, when the Poll object is no longer needed. This should only be accessed when there is no poll thread running. Use #kill instead when there is a poll loop thread running.



71
72
73
74
75
76
77
78
79
# File 'lib/0mq/poll_interruptible.rb', line 71

def close
  return nil if @dead
  
  @int_sock_rep.close
  @int_sock_req.close
  @int_context.terminate
  
  @dead = true
end

#dead?Boolean

Return true if the object has been killed or closed and cannot be run

Returns:

  • (Boolean)


82
83
84
# File 'lib/0mq/poll_interruptible.rb', line 82

def dead?
  @dead
end

#interruptObject

Interrupt the running poll loop, but do not clean up. This should be run anytime to let the poller re-evaluate state, etc.. This should only be accessed from a thread other than the poll thread,

and only if the poll thread is running


44
45
46
47
48
49
# File 'lib/0mq/poll_interruptible.rb', line 44

def interrupt
  @int_sock_req.send_string ""
  @int_sock_req.recv_array
  
  true
end

#killObject

Interrupt the running poll loop and permanently kill the Poll object This should be run once, when the Poll object is no longer needed. This should only be accessed from a thread other than the poll thread,

and only if the poll thread is running

Use #cleanup instead when there is no poll loop thread running.



56
57
58
59
60
61
62
63
64
65
# File 'lib/0mq/poll_interruptible.rb', line 56

def kill
  return nil if @dead
  
  @int_sock_req.send_array ["KILL"]
  @int_sock_req.recv_array
  @int_sock_req.close
  @int_context.terminate
  
  @dead = true
end

#run(&block) ⇒ Object

Same as Poll#run, but will yield [nil, nil] to the block if interrupted



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/0mq/poll_interruptible.rb', line 23

def run(&block)
  raise "#{self} cannot run; it was permanently killed." if @dead
  
  super do |socket, revents|
    if socket == @int_sock_rep
      result = socket.recv_array
      
      block.call nil, nil if block
      
      socket.send_array ["OKAY"]
      @int_sock_rep.close if result == ["KILL"]
    else
      block.call socket, revents if block
    end
  end.tap { |hash| hash.delete @int_sock_rep }
end