Arjun Arjun - 3 months ago 16
Python Question

how to call a method and make it run in background in python 3.4?

I have implemented the Google Cloud Messaging server in python and I want that method to be Asynchronous. I do not expect any return values from that method. Is there a simple way to do this?
I have tried using

async
from
asyncio
package:

...
loop = asyncio.get_event_loop()
if(module_status=="Fail"):
loop.run_until_complete(sendNotification(module_name, module_status))
...


and here is my method
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!")

Answer

You could use a ThreadPoolExecutor:

from concurrent.futures import ThreadPoolExecutor
executor = ThreadPoolExecutor()
...
future = executor.submit(send_notification, module_name, module_status)
Comments