parallel.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright 2011 Sybren A. Stüvel <sybren@stuvel.eu>
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # https://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. """Functions for parallel computation on multiple cores.
  17. Introduced in Python-RSA 3.1.
  18. .. note::
  19. Requires Python 2.6 or newer.
  20. """
  21. from __future__ import print_function
  22. import multiprocessing as mp
  23. from rsa._compat import range
  24. import rsa.prime
  25. import rsa.randnum
  26. def _find_prime(nbits, pipe):
  27. while True:
  28. integer = rsa.randnum.read_random_odd_int(nbits)
  29. # Test for primeness
  30. if rsa.prime.is_prime(integer):
  31. pipe.send(integer)
  32. return
  33. def getprime(nbits, poolsize):
  34. """Returns a prime number that can be stored in 'nbits' bits.
  35. Works in multiple threads at the same time.
  36. >>> p = getprime(128, 3)
  37. >>> rsa.prime.is_prime(p-1)
  38. False
  39. >>> rsa.prime.is_prime(p)
  40. True
  41. >>> rsa.prime.is_prime(p+1)
  42. False
  43. >>> from rsa import common
  44. >>> common.bit_size(p) == 128
  45. True
  46. """
  47. (pipe_recv, pipe_send) = mp.Pipe(duplex=False)
  48. # Create processes
  49. try:
  50. procs = [mp.Process(target=_find_prime, args=(nbits, pipe_send))
  51. for _ in range(poolsize)]
  52. # Start processes
  53. for p in procs:
  54. p.start()
  55. result = pipe_recv.recv()
  56. finally:
  57. pipe_recv.close()
  58. pipe_send.close()
  59. # Terminate processes
  60. for p in procs:
  61. p.terminate()
  62. return result
  63. __all__ = ['getprime']
  64. if __name__ == '__main__':
  65. print('Running doctests 1000x or until failure')
  66. import doctest
  67. for count in range(100):
  68. (failures, tests) = doctest.testmod()
  69. if failures:
  70. break
  71. if count % 10 == 0 and count:
  72. print('%i times' % count)
  73. print('Doctests done')