Guest User

Untitled

a guest
Feb 19th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. #!usr/bin/env python3
  2.  
  3.  
  4. # Date: 02-07-18, fb ~ 7th 2018 | Synchronocy
  5. # Project: Pooling test
  6. # Tired as hell cbf fixing, just demonstrating that it works
  7. # IDLE Python 3.6. 64-bit
  8.  
  9. #import requests
  10. import random
  11. import string
  12. from threading import Thread
  13. from queue import Queue
  14.  
  15. class Worker(Thread):
  16. """
  17. Pooling
  18. """
  19.  
  20. def __init__(self, tasks):
  21. Thread.__init__(self)
  22. self.tasks = tasks
  23. self.daemon = True
  24. self.start()
  25.  
  26. def run(self):
  27. while True:
  28. func, args, kargs = self.tasks.get()
  29. try:
  30. func(*args, **kargs)
  31. except Exception as ex:
  32. pass
  33. finally:
  34. self.tasks.task_done()
  35.  
  36. class ThreadPool:
  37. """
  38. Pooling
  39. """
  40.  
  41. def __init__(self, num_threads):
  42. self.tasks = Queue(num_threads)
  43. for _ in range(num_threads):
  44. Worker(self.tasks)
  45.  
  46. def add_task(self, func, *args, **kargs):
  47. """
  48. Add a task to be completed by the thread pool
  49. """
  50. self.tasks.put((func, args, kargs))
  51.  
  52. def map(self, func, args_list):
  53. """
  54. Map an array to the thread pool
  55. """
  56. for args in args_list:
  57. self.add_task(func, args)
  58.  
  59. def wait_completion(self):
  60. """
  61. Await completions
  62. """
  63. self.tasks.join()
  64. counter = 0
  65. count = counter + 1
  66.  
  67. def random_string_generator(size=10, chars=string.ascii_lowercase + string.digits):
  68. a = ''.join(random.choice(chars) for _ in range(size))
  69. with open("b1g.txt","a") as handle:
  70. handle.write(a+"\n")
  71. handle.close()
  72.  
  73. def apples():
  74. fn = 1
  75. fm = 1
  76. while True:
  77. print(fn)
  78. print(fm)
  79. fn = fn + fm
  80. fm = fm + fn
  81. random_string_generator()
  82.  
  83.  
  84. def main():
  85. apples()
  86. pool = ThreadPool(500)
  87. pool.add_task(apples)
  88. pool.wait_completion()
  89.  
  90. if __name__ == '__main__':
  91. main()
Add Comment
Please, Sign In to add comment