-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproducer_main.py
executable file
·43 lines (30 loc) · 1.47 KB
/
producer_main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/usr/local/var/pyenv/shims/python3
import pika
import datetime
import json
def connectPika():
# コネクション作成
pika_param = pika.ConnectionParameters('0.0.0.0')
connection = pika.BlockingConnection(pika_param)
# チャンネル作成
channel = connection.channel()
# キューの作成
channel.queue_declare(queue='hello')
properties = pika.BasicProperties(content_type='application/json')
# メッセージの送信
dt_now = datetime.datetime.now()
channel.basic_publish(exchange="", routing_key="hello", body=json.dumps({'title':"1hello wolrd {}!".format(dt_now)}), properties=properties)
dt_now = datetime.datetime.now()
channel.basic_publish(exchange="", routing_key="hello", body=json.dumps({'title':"2hello wolrd {}!".format(dt_now)}), properties=properties)
dt_now = datetime.datetime.now()
channel.basic_publish(exchange="", routing_key="hello", body=json.dumps({'title':"3hello wolrd {}!".format(dt_now)}), properties=properties)
dt_now = datetime.datetime.now()
channel.basic_publish(exchange="", routing_key="hello", body=json.dumps({'title':"4hello wolrd {}!".format(dt_now)}), properties=properties)
dt_now = datetime.datetime.now()
channel.basic_publish(exchange="", routing_key="hello", body=json.dumps({'title':"5hello wolrd {}!".format(dt_now)}), properties=properties)
# コネクション終了
connection.close()
def main():
connectPika()
if __name__ == '__main__':
main()