Guest User

Untitled

a guest
Aug 21st, 2018
959
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.73 KB | None | 0 0
  1. import threading
  2. import boto3
  3. import os
  4. import base64
  5. import time
  6. import random
  7. import psutil
  8.  
  9. BUCKET = ''
  10.  
  11. MIN_WAIT = 1
  12. MAX_WAIT = 20
  13.  
  14.  
  15. class Boto3Thread(threading.Thread):
  16.     daemon = True
  17.     is_running = True
  18.  
  19.     def run(self):
  20.         #session = boto3.Session()
  21.         #s3 = session.resource('s3')
  22.  
  23.         path = 'test_boto/'
  24.  
  25.         while self.is_running:
  26.             file_name = path + 'file_' + str(random.randrange(100000))
  27.             content = base64.b64encode(os.urandom(100000)).decode()
  28.  
  29.             #obj = s3.Object(BUCKET, file_name)
  30.             #obj.put(Body=content, Metadata={'Content-Type': 'text/plain'})
  31.  
  32.             #obj.delete()
  33.  
  34.             if not self.is_running:
  35.                 # Avoid an useless sleep cycle
  36.                 break
  37.  
  38.             sleep_duration = random.randrange(MIN_WAIT, MAX_WAIT)
  39.             #print('{} will sleep for {} seconds'.format(self.name, sleep_duration))
  40.             time.sleep(sleep_duration)
  41.  
  42.  
  43. def check_memory():
  44.     import gc
  45.     gc.collect()
  46.  
  47.     process = psutil.Process(os.getpid())
  48.     return process.memory_info().rss / 1024. / 1024.
  49.  
  50.  
  51. def run_pool(size):
  52.     ts = []
  53.     for x in range(size):
  54.         t = Boto3Thread()
  55.         t.start()
  56.         ts.append(t)
  57.     return ts
  58.  
  59.  
  60. def stop_pool(ts):
  61.     for t in ts:
  62.         t.is_running = False
  63.  
  64.     for t in ts:
  65.         t.join()
  66.  
  67.  
  68. def main():
  69.     ts = run_pool(100)
  70.  
  71.     try:
  72.         while True:
  73.             print('Process Memory: {:.1f} MB'.format(check_memory()))
  74.             time.sleep(5)
  75.     except KeyboardInterrupt:
  76.         pass
  77.     finally:
  78.         print('Wait for all threads to finish. Should take about {} seconds!'.format(MAX_WAIT))
  79.         stop_pool(ts)
  80.  
  81.  
  82. main()
Advertisement
Add Comment
Please, Sign In to add comment