device.py 950 B

1234567891011121314151617181920212223242526272829303132
  1. # Copyright (C) PyZMQ Developers
  2. # Distributed under the terms of the Modified BSD License.
  3. import zmq
  4. from zmq.green import Poller
  5. def device(device_type, isocket, osocket):
  6. """Start a zeromq device (gevent-compatible).
  7. Unlike the true zmq.device, this does not release the GIL.
  8. Parameters
  9. ----------
  10. device_type : (QUEUE, FORWARDER, STREAMER)
  11. The type of device to start (ignored).
  12. isocket : Socket
  13. The Socket instance for the incoming traffic.
  14. osocket : Socket
  15. The Socket instance for the outbound traffic.
  16. """
  17. p = Poller()
  18. if osocket == -1:
  19. osocket = isocket
  20. p.register(isocket, zmq.POLLIN)
  21. p.register(osocket, zmq.POLLIN)
  22. while True:
  23. events = dict(p.poll())
  24. if isocket in events:
  25. osocket.send_multipart(isocket.recv_multipart())
  26. if osocket in events:
  27. isocket.send_multipart(osocket.recv_multipart())