Created
July 9, 2022 23:19
-
-
Save ronaldpetty/4e91aadc887d46dcc8d4abceb0482a4c to your computer and use it in GitHub Desktop.
almost works - dinner
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from multiprocessing import Pool | |
from pymongo import MongoClient | |
import datetime | |
import sys | |
def insert(x): | |
print("insert") | |
print(f"instance {x}") | |
client = MongoClient('172.17.0.2', 27017) | |
db = client.test_database | |
collection = db.test_collection | |
collection.drop() | |
for i in range(10000): | |
post = {"author": "Mike", | |
"text": "My first blog post!", | |
"tags": ["mongodb", "python", "pymongo"], | |
"date": datetime.datetime.utcnow(), | |
"_id": str(x) + str(i)} | |
collection.insert_one(post) | |
def query(x): | |
print("query") | |
print(f"instance {x}") | |
client = MongoClient('172.17.0.2', 27017) | |
db = client.test_database | |
collection = db.test_collection | |
for i in range(1000): | |
id = str(x) + str(i) | |
result = collection.find_one({"_id": id}) | |
if i % 100: | |
print(f"{x}{id}:{result}") | |
if __name__ == '__main__': | |
num_insert_procs = int(sys.argv[1]) | |
num_read_procs = int(sys.argv[2]) | |
with Pool() as p: | |
p.map(insert, list(range(1, num_insert_procs+1))) | |
p.close() | |
p.join() | |
with Pool() as p2: | |
p2.map(query, list(range(1, num_read_procs+1))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment