我已经用 python 实现了 Google Cloud Messaging 服务器,我希望该方法是异步的。我不期望该方法有任何返回值。有没有一种简单的方法可以做到这一点?
我尝试使用 asyncio
包中的 async
:
...
loop = asyncio.get_event_loop()
if(module_status=="Fail"):
loop.run_until_complete(sendNotification(module_name, module_status))
...
这是我的方法sendNotification()
:
async def sendNotification(module_name, module_status):
gcm = GCM("API_Key")
data ={"message":module_status, "moduleName":module_name}
reg_ids = ["device_tokens"]
response = gcm.json_request(registration_ids=reg_ids, data=data)
print("GCM notification sent!")
最佳答案
您可以使用 ThreadPoolExecutor :
from concurrent.futures import ThreadPoolExecutor
def send_notification(module_name, module_status):
[...]
with ThreadPoolExecutor() as executor:
future = executor.submit(send_notification, module_name, module_status)
关于python - 如何在 python 3.4 中调用方法并使其在后台运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39025511/