我已经在python中实现了Google Cloud Messaging服务器,并且我希望该方法是异步的。我不希望任何来自该方法的返回值。有没有简单的方法来做到这一点? 我一直在使用async
从asyncio
包的尝试:如何调用一个方法,并使其在Python 3.4中的后台运行?
...
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!")
如果'gcm.json_request'方法没有使用'asyncio'定义,那么没有简单的方法来做到这一点。 –
@NateMara我正在使用python-gcm librabry [link](https://github.com/geeknam/python-gcm/blob/master/gcm/gcm.py)。我看到它的代码,它不是异步的。你能提出一个解决方案,让我的方法在后台运行吗? – Arjun
你可以使用'multiprocessing'或者使用'aiohttp'自己进行HTTP调用 –