Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from multiprocessing import Process
- import threading
- import os
- DIRECTORY = 'experiment'
- def create_file(_id):
- os.makedirs(DIRECTORY, exist_ok=True)
- filename = '/file_by_{}'.format(_id)
- f = open(DIRECTORY+filename, 'w')
- f.close()
- class FileWriterThread(threading.Thread):
- def __init__(self, thread_id):
- super(FileWriterThread, self).__init__()
- self._id= thread_id
- def run(self):
- create_file(self._id)
- def thread_fn(num_thread):
- thread = []
- for i in range(num_thread):
- thread.append(FileWriterThread(i))
- thread[i].start()
- for t in thread:
- t.join()
- def process_fn(num_process):
- process = []
- for i in range(num_process):
- process.append(Process(target=create_file, args=(i*-1, )))
- process[i].start()
- for p in process:
- p.join()
- if __name__ == '__main__':
- thread_fn(5)
- process_fn(5)
- print('Done')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement