如何在 python 3.4 中调用方法并使其在后台 运行?
how to call a method and make it run in background in python 3.4?
我已经在 python 中实现了 Google 云消息服务器,我希望该方法是异步的。我不希望该方法有任何 return 值。有没有简单的方法可以做到这一点?
我尝试使用 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)
由于 GCM 不兼容异步库,因此需要使用外部事件循环。
有几个,最简单的 IMO 可能是 gevent。
请注意,如果使用的底层库依赖于阻塞行为来运行,gevent 猴子补丁可能会引入死锁。
import gevent
from gevent.greenlet import Greenlet
from gevent import monkey
monkey.patch_all()
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!")
greenlet = Greenlet.spawn(sendNotification,
args=(module_name, module_status,))
# Yield control to gevent's event loop without blocking
# to allow background tasks to run
gevent.sleep(0)
#
# Other code, other greenlets etc here
#
# Call get to get return value if needed
greenlet.get()
你可以使用asyncio的api:loop.run_in_executor(None, callable)
这将 运行 使用执行器的代码(默认为 ThreadPoolExecutor)
我已经在 python 中实现了 Google 云消息服务器,我希望该方法是异步的。我不希望该方法有任何 return 值。有没有简单的方法可以做到这一点?
我尝试使用 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)
由于 GCM 不兼容异步库,因此需要使用外部事件循环。
有几个,最简单的 IMO 可能是 gevent。
请注意,如果使用的底层库依赖于阻塞行为来运行,gevent 猴子补丁可能会引入死锁。
import gevent
from gevent.greenlet import Greenlet
from gevent import monkey
monkey.patch_all()
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!")
greenlet = Greenlet.spawn(sendNotification,
args=(module_name, module_status,))
# Yield control to gevent's event loop without blocking
# to allow background tasks to run
gevent.sleep(0)
#
# Other code, other greenlets etc here
#
# Call get to get return value if needed
greenlet.get()
你可以使用asyncio的api:loop.run_in_executor(None, callable)
这将 运行 使用执行器的代码(默认为 ThreadPoolExecutor)