我创建了一个订户客户端,以侦听来自pubsub仿真器的消息。当我向pubsub发布500多条消息时。尽管已经确认了这些消息,但订阅者客户端仍然收到了消息。
我正在使用python 3。
共有3个课程:
A类:订阅者客户端侦听来自pubsub仿真器的消息
B类:用于将消息存储到云存储的对象
C类:用于将消息提取并插入到SQL表中的对象
from google.cloud import pubsub_v1
import B
import C
class A(object):
def receive_messages(self,project_id, subscription_name):
subscriber = pubsub_v1.SubscriberClient()
subscription_path = subscriber.subscription_path(
project_id, subscription_name)
def callback(message):
print('Received message: {}'.format(message))
B_obj = B()
storing_status = B_obj.storing_to_cloud_drive()
if storing_status:
message.ack()
print('Message is acked')
C_obj = C()
C_obj.extract_and_insert_to_sql_table()
else:
message.nack()
print('Message is nacked')
subscriber.subscribe(subscription_path, callback=callback)
print('Listening for messages on {}'.format(subscription_path))
while True:
time.sleep(60)