-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathThreadHomework.py
More file actions
49 lines (36 loc) · 1.14 KB
/
ThreadHomework.py
File metadata and controls
49 lines (36 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# TASK: Write 10_000_000 line text to a 2 files using threads and GIL in Python
# 1) Create a function that clears the content of a file
# 2) Create a function that writes a text to a file
# 3) Create a function that reads the content of a file
import threading
import time
the_word = "qwertyuiopasdfghjklzxcvbnm"
file_name = 'words.txt'
def writes():
with open (file_name, 'a') as file:
for item in range(9_999):
file.write(f"{the_word}\n")
file.write(the_word)
def clears():
a = 10_000
while a != 0:
with open (file_name, 'w') as file:
file.writelines(lines[:-1])
a -= 1
def reads():
with open (file_name, 'r') as file:
lines = file.readlines()
return len(lines)
def main():
start = time.perf_counter()
t1 = threading.Thread(target=writes(), name="yozuvchi")
t2 = threading.Thread(target=clears(), nmae="tozalovchi")
t1.start()
t2.start()
end = time.perf_counter()
print(t1.getName())
print(t2.getName())
print(f"The number of rows remaining {reads()}")
print(f"Time used: {start - end}")
if __name__=="__main__":
main()