Skip to content

Instantly share code, notes, and snippets.

@RenSys
Created September 12, 2017 21:50
Show Gist options
  • Save RenSys/22bcb803b47df2ec1b824c24ea515aa9 to your computer and use it in GitHub Desktop.
Save RenSys/22bcb803b47df2ec1b824c24ea515aa9 to your computer and use it in GitHub Desktop.
Python Standard - Priority Queues
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {
"ExecuteTime": {
"start_time": "2017-09-12T21:48:10.232888Z",
"end_time": "2017-09-12T21:48:10.240578Z"
},
"collapsed": true,
"trusted": true
},
"cell_type": "code",
"source": "from queue import PriorityQueue\nimport threading\nfrom time import sleep",
"execution_count": 6,
"outputs": []
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2017-09-12T21:50:18.699998Z",
"end_time": "2017-09-12T21:50:18.709814Z"
},
"trusted": true,
"collapsed": true
},
"cell_type": "code",
"source": "pq = PriorityQueue()\nlow_priority = 10 \nmedium_priority = 5 \nhigh_priority = 1",
"execution_count": 13,
"outputs": []
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2017-09-12T21:50:20.474840Z",
"end_time": "2017-09-12T21:50:20.484239Z"
},
"trusted": true
},
"cell_type": "code",
"source": "pq.put((high_priority, 'High'))\npq.put((medium_priority, 'Medium'))\npq.put((low_priority, 'Low'))\n",
"execution_count": 14,
"outputs": []
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2017-09-12T21:50:21.250111Z",
"end_time": "2017-09-12T21:50:27.265753Z"
},
"trusted": true
},
"cell_type": "code",
"source": "while not pq.empty():\n sleep(2)\n print('reading {} from que'.format(pq.get()))",
"execution_count": 15,
"outputs": [
{
"output_type": "stream",
"text": "reading (1, 'High') from que\nreading (5, 'Medium') from que\nreading (10, 'Low') from que\n",
"name": "stdout"
}
]
},
{
"metadata": {
"collapsed": true,
"trusted": true
},
"cell_type": "code",
"source": "",
"execution_count": null,
"outputs": []
}
],
"metadata": {
"kernelspec": {
"name": "py36-test",
"display_name": "py36-test",
"language": "python"
},
"_draft": {
"nbviewer_url": "https://gist.github.com/bf7cb1df3941dc48a16fdd6296f5da93"
},
"language_info": {
"mimetype": "text/x-python",
"nbconvert_exporter": "python",
"name": "python",
"pygments_lexer": "ipython3",
"version": "3.6.1",
"file_extension": ".py",
"codemirror_mode": {
"version": 3,
"name": "ipython"
}
},
"gist": {
"id": "bf7cb1df3941dc48a16fdd6296f5da93",
"data": {
"description": "Python Standard - Priority Queues",
"public": true
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment