Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

connection.py 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #
  2. # Analogue of `multiprocessing.connection` which uses queues instead of sockets
  3. #
  4. # multiprocessing/dummy/connection.py
  5. #
  6. # Copyright (c) 2006-2008, R Oudkerk
  7. # All rights reserved.
  8. #
  9. # Redistribution and use in source and binary forms, with or without
  10. # modification, are permitted provided that the following conditions
  11. # are met:
  12. #
  13. # 1. Redistributions of source code must retain the above copyright
  14. # notice, this list of conditions and the following disclaimer.
  15. # 2. Redistributions in binary form must reproduce the above copyright
  16. # notice, this list of conditions and the following disclaimer in the
  17. # documentation and/or other materials provided with the distribution.
  18. # 3. Neither the name of author nor the names of any contributors may be
  19. # used to endorse or promote products derived from this software
  20. # without specific prior written permission.
  21. #
  22. # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND
  23. # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  26. # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  27. # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  28. # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  29. # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  31. # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  32. # SUCH DAMAGE.
  33. #
  34. from __future__ import absolute_import
  35. from billiard.five import Queue
  36. __all__ = ['Client', 'Listener', 'Pipe']
  37. families = [None]
  38. class Listener(object):
  39. def __init__(self, address=None, family=None, backlog=1):
  40. self._backlog_queue = Queue(backlog)
  41. def accept(self):
  42. return Connection(*self._backlog_queue.get())
  43. def close(self):
  44. self._backlog_queue = None
  45. address = property(lambda self: self._backlog_queue)
  46. def __enter__(self):
  47. return self
  48. def __exit__(self, *exc_info):
  49. self.close()
  50. def Client(address):
  51. _in, _out = Queue(), Queue()
  52. address.put((_out, _in))
  53. return Connection(_in, _out)
  54. def Pipe(duplex=True):
  55. a, b = Queue(), Queue()
  56. return Connection(a, b), Connection(b, a)
  57. class Connection(object):
  58. def __init__(self, _in, _out):
  59. self._out = _out
  60. self._in = _in
  61. self.send = self.send_bytes = _out.put
  62. self.recv = self.recv_bytes = _in.get
  63. def poll(self, timeout=0.0):
  64. if self._in.qsize() > 0:
  65. return True
  66. if timeout <= 0.0:
  67. return False
  68. self._in.not_empty.acquire()
  69. self._in.not_empty.wait(timeout)
  70. self._in.not_empty.release()
  71. return self._in.qsize() > 0
  72. def close(self):
  73. pass