{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import pymongo"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"mongoClient = pymongo.MongoClient()\n",
"mongoDB = mongoClient.testDB\n",
"mongoCollection = mongoDB.testCollection"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"import datetime\n",
"post = {\"author\": \"Mike\",\n",
" \"text\": \"My first blog post!\",\n",
" \"tags\": [\"mongodb\", \"python\", \"pymongo\"],\n",
" \"date\": datetime.datetime.utcnow()}"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"ename": "NameError",
"evalue": "name 'db' is not defined",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32mf:\\Jianshun\\analyseScript\\testMongoDB.ipynb Cell 4\u001b[0m in \u001b[0;36m1\n\u001b[1;32m----> 1\u001b[0m posts \u001b[39m=\u001b[39m db\u001b[39m.\u001b[39mposts\n\u001b[0;32m 2\u001b[0m post_id \u001b[39m=\u001b[39m posts\u001b[39m.\u001b[39minsert_one(post)\u001b[39m.\u001b[39minserted_id\n\u001b[0;32m 3\u001b[0m post_id\n",
"\u001b[1;31mNameError\u001b[0m: name 'db' is not defined"
]
}
],
"source": [
"posts = mongoCollection.posts\n",
"post_id = posts.insert_one(post).inserted_id\n",
"post_id"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Task executing\n",
"\n",
"Task2 executing \n",
"\n",
"Task2 done\n",
"\n",
"Task done\n",
"\n"
]
}
],
"source": [
"from time import sleep\n",
"from multiprocessing.pool import ThreadPool\n",
" \n",
"# task executed in a worker thread\n",
"def task():\n",
" # report a message\n",
" print(f'Task executing\\n')\n",
" # block for a moment\n",
" sleep(1)\n",
" # report a message\n",
" print(f'Task done\\n')\n",
" \n",
"def task2():\n",
" # report a message\n",
" print(f'Task2 executing \\n')\n",
" # block for a moment\n",
" sleep(1)\n",
" # report a message\n",
" print(f'Task2 done\\n')\n",
" \n",
"# protect the entry point\n",
"if __name__ == '__main__':\n",
" # create and configure the thread pool\n",
" pool = ThreadPool()\n",
" # issue tasks to the thread pool\n",
" pool.apply_async(task)\n",
" pool.apply_async(task2)\n",
" # close the thread pool\n",
" pool.close()\n",
" # wait for all tasks to finish\n",
" pool.join()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "py39",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}