-
Notifications
You must be signed in to change notification settings - Fork 0
/
dock.py
128 lines (101 loc) · 3.6 KB
/
dock.py
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import threading
import glob
import subprocess
import time
import psutil
import resource
import uuid
import gc
from threading import Barrier
class DockingProcessor:
def __init__(self):
self.FILES = glob.glob("*.pdbqt")
self.barrier = Barrier(6)
self.event = threading.Event()
def memory_monitor(self, threshold):
while True:
available_memory = psutil.virtual_memory().available
if available_memory > threshold:
break
time.sleep(1)
def process_files(self):
threads = []
completed_threads = 0
lock = threading.Lock()
def thread_callback():
nonlocal completed_threads
with lock:
completed_threads += 1
for i in range(0, len(self.FILES), 100):
bunch = self.FILES[i:i+100]
thread = ProcessFileThread(bunch, self.barrier, self.event, thread_callback)
threads.append(thread)
threshold = 1024
while True:
available_memory = psutil.virtual_memory().available
if available_memory > threshold:
break
time.sleep(1)
for thread in threads:
thread.start()
while completed_threads < len(threads):
time.sleep(1)
gc.collect()
# After threads have completed, check memory
self.check_memory()
def check_memory(self):
bunch = []
for i in range(0, len(self.FILES), 6):
current_bunch = self.FILES[i:i+6]
bunch.extend(current_bunch)
memory_info = psutil.Process().memory_info()
print(f"Memory usage: {memory_info.rss / (1024 * 1024)} MB")
if memory_info.rss > 22528000:
print("Memory usage exceeded the limit.")
self.event.set()
self.barrier.wait()
break
print(f"Bunch loaded.")
def run(self):
self.process_files()
class ProcessFileThread(threading.Thread):
def __init__(self, bunch, barrier, event, callback):
super().__init__()
self.bunch = bunch
self.barrier = barrier
self.event = event
self.callback = callback
def run(self):
try:
for f in self.bunch:
subprocess.run([
"/mnt/c/Users/taha_/OneDrive/Belgeler/docking/autodock_vina_1_1_2_linux_x86/bin/vina",
"--receptor", "/mnt/g/4h10_edited-autodock-with-remark.pdbqt",
"--ligand", f"{f}",
"--center_x", "20",
"--center_y", "-15",
"--center_z", "0",
"--size_x", "30",
"--size_y", "30",
"--size_z", "30",
"--cpu", "6",
"--out", f"{f}_{uuid.uuid4()}.pdbqt"
])
# Check memory usage
memory_info = psutil.Process().memory_info()
print(f"Memory usage: {memory_info.rss / (1024 * 1024)} GB")
# Set event if memory usage is above the limit
if memory_info.rss > 2252800000000:
print("Memory usage exceeded the limit.")
self.event.set()
self.barrier.wait()
except Exception as e:
print(e)
gc.collect()
# Delete the files
del self.bunch
# Notify the main thread that this thread has completed its work
self.callback()
if __name__ == "__main__":
processor = DockingProcessor()
processor.run()