I adapted this code for using Google Cloud PubSub in Async Python: https://github.com/cloudfind/google-pubsub-asyncio
import asyncio
import datetime
import functools
import os
from google.cloud import pubsub
from google.gax.errors import RetryError
from grpc import StatusCode
async def message_producer():
""" Publish messages which consist of the current datetime """
while True:
await asyncio.sleep(0.1)
async def proc_message(message):
await asyncio.sleep(0.1)
print(message)
message.ack()
def main():
""" Main program """
loop = asyncio.get_event_loop()
topic = "projects/{project_id}/topics/{topic}".format(
project_id=PROJECT, topic=TOPIC)
subscription_name = "projects/{project_id}/subscriptions/{subscription}".format(
project_id=PROJECT, subscription=SUBSCRIPTION)
subscription = make_subscription(
topic, subscription_name)
def create_proc_message_task(message):
""" Callback handler for the subscription; schedule a task on the event loop """
print("Task created!")
task = loop.create_task(proc_message(message))
subscription.open(create_proc_message_task)
# Produce some messages to consume
loop.create_task(message_producer())
print("Subscribed, let's do this!")
loop.run_forever()
def make_subscription(topic, subscription_name):
""" Make a publisher and subscriber client, and create the necessary resources """
subscriber = pubsub.SubscriberClient()
try:
subscriber.create_subscription(subscription_name, topic)
except:
pass
subscription = subscriber.subscribe(subscription_name)
return subscription
if __name__ == "__main__":
main()
I basically removed the publishing code and only use the subscription code.
However, initially I did not include the loop.create_task(message_producer())
line. I figured that tasks were created as they were supposed to however they never actually run themselves. Only if I add said line the code properly executes and all created Tasks run. What causes this behaviour?
答案 0 :(得分:4)
PubSub正在从另一个线程调用create_proc_message_task
回调。由于create_task
是not thread-safe,因此只能从运行事件循环的线程(通常是主线程)调用它。要解决此问题,请将loop.create_task(proc_message(message))
替换为asyncio.run_coroutine_threadsafe(proc_message(message), loop)
,将不再需要message_producer
。
至于为什么message_producer
似乎修复了代码,请考虑run_coroutine_threadsafe
与create_task
相比还有两件事:
在你的情况下create_task
将任务添加到循环的可运行队列(没有任何锁定),但未能确保唤醒,因为在事件循环线程中运行时不需要。然后message_producer
用于强制循环以固定间隔唤醒,这时它也检查并执行可运行的任务。