new_task.py
1 #!/usr/bin/env python 2 3 import asyncio 4 import aioamqp 5 6 import sys 7 8 9 @asyncio.coroutine 10 def new_task(): 11 try: 12 transport, protocol = yield from aioamqp.connect('localhost', 5672) 13 except aioamqp.AmqpClosedConnection: 14 print("closed connections") 15 return 16 17 18 channel = yield from protocol.channel() 19 20 yield from channel.queue('task_queue', durable=True) 21 22 message = ' '.join(sys.argv[1:]) or "Hello World!" 23 24 yield from channel.basic_publish( 25 payload=message, 26 exchange_name='', 27 routing_key='task_queue', 28 properties={ 29 'delivery_mode': 2, 30 }, 31 ) 32 print(" [x] Sent %r" % message,) 33 34 yield from protocol.close() 35 transport.close() 36 37 38 asyncio.get_event_loop().run_until_complete(new_task()) 39 40