device.py 978 B

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