Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
coolamqp
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
public
coolamqp
Merge requests
!6
CoolAMQP 2.0.0
Code
Review changes
Check out branch
Download
Patches
Plain diff
Merged
CoolAMQP 2.0.0
milestone-2.0.0
into
develop
Overview
0
Commits
18
Pipelines
8
Changes
32
Merged
Piotr Maślanka
requested to merge
milestone-2.0.0
into
develop
9 months ago
Overview
0
Commits
18
Pipelines
8
Changes
3
Expand
incompatible with 1.x.x
👍
0
👎
0
Merge request reports
Compare
version 7
version 7
40191cef
8 months ago
version 6
83cf7243
8 months ago
version 5
652cb57d
8 months ago
version 4
ccb59b44
9 months ago
version 3
51fd0343
9 months ago
version 2
e80bad62
9 months ago
version 1
4ac48845
9 months ago
develop (base)
and
latest version
latest version
f0d69835
18 commits,
8 months ago
version 7
40191cef
17 commits,
8 months ago
version 6
83cf7243
15 commits,
8 months ago
version 5
652cb57d
13 commits,
8 months ago
version 4
ccb59b44
11 commits,
9 months ago
version 3
51fd0343
9 commits,
9 months ago
version 2
e80bad62
9 commits,
9 months ago
version 1
4ac48845
8 commits,
9 months ago
Show latest version
3 files
+
61
−
1
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
3
Search (e.g. *.vue) (Ctrl+P)
docs/tutorials/send_and_receive.rst
0 → 100644
+
59
−
0
Options
Send and receive
================
In this tutorial we'll learn how to declare a named queue and send a message to it with acknowledgement:
First we need to connect to the server. Let's assume you have configured a virtual host called /vhost
.. code-block:: python
from coolamqp.cluster import Cluster
from coolamqp.objects import NodeDefinition
nd = NodeDefinition('amqp://127.0.0.1:5672/vhost', user='test', password='test', heartbeat=30)
c = Cluster(nd)
c.start()
Then we'll need to declare a queue:
.. code-block:: python
from coolamqp.objects import Queue
queue = Queue('my-named-queue')
c.declare(queue).result()
You'll be calling :code:`result()` on most of CoolAMQP's calls, as they return futures that complete when the task is done.
Now let's try subscribing to this queue:
.. code-block:: python
def handle_message(msg):
print(msg.body.tobytes().encode('utf-8'))
msg.ack()
cons, fut = c.consume(queue, no_ack=False, on_message=handle_message)
fut.result()
Notice the :code:`tobytes()`. This is because CoolAMQP by default returns most of it's received bytes as memoryviews,
for speed's sake.
Also, your message handler is executed within the CoolAMQP's connection thread, so don't block for too long.
Now it's time to send a message:
.. code-block:: python
from coolamqp.objects import Message
c.publish(Message(b'my bag of bytes'), confirm=True).result()
Without the confirm flag, publish would not return the future.
Congratulations, you've just send and received a message! Now it's time to do some cleanup. First, we cancel the consumer,
and then disconnect from the server
.. code-block:: python
cons.cancel().result()
c.shutdown()
Loading