Skip to content

Instantly share code, notes, and snippets.

@wtolson
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wtolson/d5955956972b7bb901ef to your computer and use it in GitHub Desktop.
Save wtolson/d5955956972b7bb901ef to your computer and use it in GitHub Desktop.
Nsq and Python
Display the source blob
Display the rendered blob
Raw
{
"metadata": {
"name": "",
"signature": "sha256:a4cf7750eed37e330138885cbaed7872a2c4b4a40f551a6aa4db1265286f1ab1"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Nsq and Python\n",
"Trevor Olson <br>\n",
"[@ZombieFeynman](https://twitter.com/ZombieFeynman)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Docs\n",
"* http://nsq.io/\n",
"* http://gnsq.readthedocs.org/\n",
"* https://pynsq.readthedocs.org/\n",
"* http://is.gd/python_gnsq"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Download Nsq\n",
" \n",
"```bash\n",
"wget https://s3.amazonaws.com/bitly-downloads/nsq/nsq-0.2.28.darwin-amd64.go1.2.1.tar.gz\n",
"tar xzf nsq-0.2.28.darwin-amd64.go1.2.1.tar.gz\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Run nsqd\n",
"```bash\n",
"cd nsq-0.2.28.darwin-amd64.go1.2.1\n",
"./bin/nsqd\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Install gnsq\n",
"\n",
"```bash\n",
"mkvirtualenv nsqdemo\n",
"pip install gnsq\n",
"```"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import gnsq\n",
"conn = gnsq.Nsqd('localhost', 4150)"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Publish a message\n",
"```bash\n",
"curl -d 'hello world!' 'http://127.0.0.1:4151/put?topic=test'\n",
"```"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"conn.publish('test', 'hello world!')"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Publish lots of messages"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"messages = ['{} bottles of beer on the wall'.format(count) for count in xrange(99, 0, -1)]\n",
"conn.multipublish('beers', messages)"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Read our messages back"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Read our messages\n",
"import gevent\n",
"reader = gnsq.Reader('beers', 'singer', nsqd_tcp_addresses=['localhost:4150'])"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"@reader.on_message.connect\n",
"def sing(reader, message):\n",
" print message.body + '!'\n",
" print message.body + ','\n",
" print message.body + '.'\n",
" print 'Take one down, pass it around...'\n",
" \n",
" if message.body.startswith('1 '):\n",
" print '0 bottles of beer on the wall!!!!!'\n",
" gevent.spawn(reader.close)"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"reader.start()"
],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment