title: is there a way to have a producer and consumer with aio-pika
body: <p>I have an app that requires a consumer and producer to run both that at the same time. I have tried to code something but have an attribute error</p>
  <p>Googling this doesn't show many results in terms of this working and I was wondering if anyone else had managed to do what I'm trying to do with asyncio and pika.</p>
  <p>Here is what I have at the moment but I think I'm very much some way off</p>
  <pre class="lang-py prettyprint-override"><code>async def consumer(loop):
      connection = await aio_pika.connect_robust(
          &quot;amqp://guest:guest@127.0.0.1/&quot;, loop=loop
      )
  
      async with connection:
          queue_name = &quot;test_queue&quot;
  
          # Creating channel
          channel = await connection.channel()    # type: aio_pika.Channel
  
          # Declaring queue
          queue = await channel.declare_queue(
              queue_name,
              auto_delete=True
          )   # type: aio_pika.Queue
  
          async with queue.iterator() as queue_iter:
              # Cancel consuming after __aexit__
              async for message in queue_iter:
                  async with message.process():
                      print(message.body)
  
                      if queue.name in message.body.decode():
                          break
          print('Connected to RabbitMQ')
  
  
  async def producer(loop):
      connection = await aio_pika.connect_robust(
          &quot;amqp://guest:guest@127.0.0.1/&quot;, loop=loop
      )
  
      routing_key = &quot;test_queue&quot;
  
      channel = await connection.channel()    # type: aio_pika.Channel
  
      await channel.default_exchange.publish(
          aio_pika.Message(
              body='Hello {}'.format(routing_key).encode()
          ),
          routing_key=routing_key
      )
  
      await connection.close()
  
  
  async def main():
  
      # You can set a max size if you want to prevent pull too many messages from SQS.
      queue = asyncio.Queue()
  
      tasks = asyncio.gather(
          producer(queue), *[consumer(queue) for _ in range(3)])
  
  
  asyncio.run(main())
  </code></pre>
  <p>I'm also very new on my Python journey so this code above may be a little off.</p>