-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreading.py
More file actions
50 lines (40 loc) · 1.07 KB
/
threading.py
File metadata and controls
50 lines (40 loc) · 1.07 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
50
import threading
import time, random
def f(line):
"""
A function that performs a CPU-bound operation.
Parameters:
- line (str): The line to be executed.
Returns:
None
"""
# Your CPU-bound operation here
print(f"Executing: {line}")
time.sleep(4**random.random())
print(f"Finish excecutong: {line}")
def parallel_f(lines):
"""
Executes a list of functions in parallel using multiple threads.
Args:
lines (List[str]): A list of strings representing the lines to be processed.
Returns:
None
"""
threads = []
for line in lines:
thread = threading.Thread(target=f, args=(line,))
thread.start()
threads.append(thread)
# Wait for all threads to finish
for thread in threads:
thread.join()
# Usage
lines = ["a", "b", "c"] # Your list of input strings
parallel_f(lines)
print(lines)
print(type(lines))
print(lines.__repr__())
print(type(lines.__repr__()))
print(eval(lines.__repr__()))
print(type(eval(lines.__repr__())))
print(type(lines.__str__()))