-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmain.py
More file actions
59 lines (49 loc) · 1.92 KB
/
Copy pathmain.py
File metadata and controls
59 lines (49 loc) · 1.92 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
51
52
53
54
55
56
57
58
59
import pymongo
import concurrent.futures
import subprocess
import shutil
# Replace these variables with your MongoDB connection details
mongo_host = "127.0.0.1"
mongo_port = 27017 # Default MongoDB port
mongo_database = "bookUrlList"
mongo_collection = "books"
# Create a MongoDB client
client = pymongo.MongoClient(host=mongo_host, port=mongo_port)
# Access your database
database = client[mongo_database]
# Access your collection
collection = database[mongo_collection]
all_documents = collection.find()
# Function to run script2.py and return "success" or "failed"
def run_script(argument):
try:
# Execute run.py
book_id = argument["id"]
book_title = argument["title"]
book_url = argument["url"]
cache_dir = book_url.split('/')[5]
process = subprocess.Popen(["python", "run.py", book_url], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = process.communicate()
# Check if run.py ran successfully
if process.returncode == 0:
return f"successfully download {book_id} {book_title}"
else:
shutil.rmtree(cache_dir)
return f"failed download {book_id} {book_title}"
except Exception as e:
shutil.rmtree(cache_dir)
return f"failed download {book_id} {book_title}"
# Number of total script2.py instances
total_instances = 2297673
# Number of instances to run concurrently
concurrent_limit = 2
# Create a ThreadPoolExecutor with the concurrent limit
with concurrent.futures.ThreadPoolExecutor(max_workers=concurrent_limit) as executor:
# Submit tasks to run script2.py concurrently
futures = [executor.submit(run_script, arg) for arg in all_documents]
# Wait for all tasks to complete and print results
for future in concurrent.futures.as_completed(futures):
result = future.result()
print(result)
# Don't forget to close the MongoDB connection when you're done
client.close()