SHARE
TWEET

Untitled

a guest Oct 20th, 2019 86 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from multiprocessing import Process
  2. import threading
  3. import os
  4.  
  5.  
  6. DIRECTORY = 'experiment'
  7.  
  8.  
  9. def create_file(_id):
  10.     os.makedirs(DIRECTORY, exist_ok=True)
  11.     filename = '/file_by_{}'.format(_id)
  12.     f = open(DIRECTORY+filename, 'w')
  13.     f.close()
  14.  
  15.  
  16. class FileWriterThread(threading.Thread):
  17.      def __init__(self, thread_id):
  18.         super(FileWriterThread, self).__init__()
  19.         self._id= thread_id
  20.  
  21.      def run(self):
  22.         create_file(self._id)
  23.  
  24.  
  25. def thread_fn(num_thread):
  26.     thread = []
  27.     for i in range(num_thread):
  28.         thread.append(FileWriterThread(i))
  29.         thread[i].start()
  30.  
  31.     for t in thread:
  32.         t.join()
  33.  
  34. def process_fn(num_process):
  35.     process = []
  36.     for i in range(num_process):
  37.         process.append(Process(target=create_file, args=(i*-1, )))
  38.         process[i].start()
  39.  
  40.     for p in process:
  41.         p.join()
  42.  
  43.  
  44. if __name__ == '__main__':
  45.     thread_fn(5)
  46.     process_fn(5)
  47.     print('Done')
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top