1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import
pika
import
sys
import
random
def
makepassword(rang
=
"23456789qwertyupasdfghjkzxcvbnm"
, size
=
8
):
return
string.join(random.sample(rang, size)).replace(
" "
,"")
parameters
=
pika.ConnectionParameters(host
=
'localhost'
)
connection
=
pika.BlockingConnection(parameters)
channel
=
connection.channel()
channel.queue_declare(queue
=
'task_queue'
, durable
=
True
)
message
=
makepassword()
channel.basic_publish(exchange
=
'',
routing_key
=
'task_queue'
,
body
=
message,
properties
=
pika.BasicProperties(
delivery_mode
=
2
,
# make message persistent
))
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import
pika
import
time
connection
=
pika.BlockingConnection(pika.ConnectionParameters(host
=
'localhost'
))
channel
=
connection.channel()
channel.queue_declare(queue
=
'task_queue'
, durable
=
True
)
print
' [*] Waiting for messages. To exit press CTRL+C'
def
callback(ch, method, properties, body):
print
" [x] Received %r"
%
(body,)
print
" [x] Done"
ch.basic_ack(delivery_tag
=
method.delivery_tag)
channel.basic_qos(prefetch_count
=
1
)
channel.basic_consume(callback,
queue
=
'task_queue'
)
channel.start_consuming()
|
1
2
3
4
5
6
7
|
def callback(ch, method, properties, body):
print
" [x] Received %r"
% (body,)
print “正在搞呀”
print
" [x] Done"
ch.basic_ack(delivery_tag = method.delivery_tag)
channel.basic_consume(callback,
queue=
'hello'
)
|
1
|
channel.queue_declare(queue=
'task_queue'
, durable=True)
|
1
|
channel.basic_qos(prefetch_count=
1
)
|