Arjun Arjun - 1 year ago 149
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


loop = asyncio.get_event_loop()
loop.run_until_complete(sendNotification(module_name, module_status))

and here is my method

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 Source

You could use a ThreadPoolExecutor:

from concurrent.futures import ThreadPoolExecutor
executor = ThreadPoolExecutor()
future = executor.submit(send_notification, module_name, module_status)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download