写一个game 循环
game loop是每个游戏的中心.它不停的获取用户输入,更新游戏状态,渲染游戏结果到屏幕上.网络游戏分为客户端和服务端两部分.两边的loop通过网络连接起来.通常情况下,客户端获取用户输入,发送到服务端,服务端处理计算数据,更新玩家状态,发送结果个客户端.比如玩家或者游戏物体的位置.非常重要的是,不要把客户端和服务端的作用混淆了,如果没有充分的理由的话. 如果在客户端做游戏计算,那么不同的客户端非常容易就不同步了.
A game loop iteration is often called a tick. Tick is an event meaning that current game loop iteration is over and the data for the next frame(s) is ready.
在下一个例子中,我们写一个客户端,这个客户端通过WebSocket连接服务器,同时运行一个简单的loop,接受输入发送给服务器,回显消息.Client source code is located here.
3.1
我们使用aiohttp来创建一个game server.这个库可以创建asyncio的client和server.这个库的好处是同时支持http请求和websocket.所以服务器就不需要把结果处理成html了.
来看一下server如何运行:
import asyncio
from aiohttp import web
async def handle(request):
index = open("index.html", 'rb')
content = index.read()
return web.Response(body=content)
async def wshandler(request):
app = request.app
ws = web.WebSocketResponse()
await ws.prepare(request)
app["sockets"].append(ws)
while 1:
msg = await ws.receive()
if msg.tp == web.MsgType.text:
print("Got message %s" % msg.data)
ws.send_str("Pressed key code: {}".format(msg.data))
elif msg.tp == web.MsgType.close or\
msg.tp == web.MsgType.error:
break
app["sockets"].remove(ws)
print("Closed connection")
return ws
async def game_loop(app):
while 1:
for ws in app["sockets"]:
ws.send_str("game loop says: tick")
await asyncio.sleep(2)
app = web.Application()
app["sockets"] = []
asyncio.ensure_future(game_loop(app))
app.router.add_route('GET', '/connect', wshandler)
app.router.add_route('GET', '/', handle)
web.run_app(app)
这个代码就不翻译了,
3.2 有请求才开始loop
上面的例子,server是不停的loop.现在改成有请求才loop. 同时,server上可能存在多个room.一个player创建了一个session(一场比赛或者一个副本?),其他的player可以加入.
import asyncio
from aiohttp import web
async def handle(request):
index = open("index.html", 'rb')
content = index.read()
return web.Response(body=content)
async def wshandler(request):
app = request.app
ws = web.WebSocketResponse()
await ws.prepare(request)
app["sockets"].append(ws)
if app["game_is_running"] == False:
asyncio.ensure_future(game_loop(app))
while 1:
msg = await ws.receive()
if msg.tp == web.MsgType.text:
print("Got message %s" % msg.data)
ws.send_str("Pressed key code: {}".format(msg.data))
elif msg.tp == web.MsgType.close or\
msg.tp == web.MsgType.error:
break
app["sockets"].remove(ws)
print("Closed connection")
return ws
async def game_loop(app):
app["game_is_running"] = True
while 1:
for ws in app["sockets"]:
ws.send_str("game loop says: tick")
if len(app["sockets"]) == 0:
break
await asyncio.sleep(2)
app["game_is_running"] = False
app = web.Application()
app["sockets"] = []
app["game_is_running"] = False
app.router.add_route('GET', '/connect', wshandler)
app.router.add_route('GET', '/', handle)
web.run_app(app)
3.3 管理task
直接操作task对象.没有人的时候,可以cancel掉task.
注意!:
This cancel()
call tells scheduler not to pass execution to this coroutine anymore and sets its state tocancelled
which then can be checked by cancelled()
method. And here is one caveat worth to mention: when you have external references to a task object and exception happens in this task, this exception will not be raised. Instead, an exception is set to this task and may be checked by exception()
method. Such silent fails are not useful when debugging a code. Thus, you may want to raise all exceptions instead. To do so you need to call result()
method of unfinished task explicitly. This can be done in a callback:
如果想要cancel掉,也不想触发exception,那么就检查一下canceled状态.
app["game_loop"].add_done_callback(lambda t: t.result() if not t.cancelled() else None)
3.4 等待多个事件
Example 3.4 source code
在很多情况下,需要在服务器处理客户端的handler中, 等待多个事件.除了等待客户端的消息,可能还需要等待不同的消息发生.比如, 游戏一局的时间到了,需要一个timer的信号.或者,需要其他进程的消息,或者其他server的消息.(使用分布式消息系统).
下面这个例子使用了Condition.这里不保存全部的socket,而是在每次循环结束通过Condition.notify_all来通知.这个使用pub/sub模式实现.
为了在一个handler中,等待两个事件,首先我们使用ensure_future来包装一下.
if not recv_task:
recv_task = asyncio.ensure_future(ws.receive())
if not tick_task:
await tick.acquire()
tick_task = asyncio.ensure_future(tick.wait())```
在我们调用Condition.call之前,我们需要获取一下锁.这个锁在调用了tick.wait之后就释放掉.这样其他的协程也可以用了.但是当我们得到一个notification, 会重新获取锁.所以我们在收到notification之后要release一下.
done, pending = await asyncio.wait( [recv_task, tick_task], return_when=asyncio.FIRST_COMPLETED)```
这个会阻塞住直到有一个任务完成,这个时候会返回两个列表,完成的和仍然在运行的.如果task is done,我们再设置为None,这样下一个循环里会再一次创建.
import asyncio
from aiohttp import web
async def handle(request):
index = open("index.html", 'rb')
content = index.read()
return web.Response(body=content)
tick = asyncio.Condition()
async def wshandler(request):
ws = web.WebSocketResponse()
await ws.prepare(request)
recv_task = None
tick_task = None
while 1:
if not recv_task:
recv_task = asyncio.ensure_future(ws.receive())
if not tick_task:
await tick.acquire()
tick_task = asyncio.ensure_future(tick.wait())
done, pending = await asyncio.wait(
[recv_task,
tick_task],
return_when=asyncio.FIRST_COMPLETED)
if recv_task in done:
msg = recv_task.result()
if msg.tp == web.MsgType.text:
print("Got message %s" % msg.data)
ws.send_str("Pressed key code: {}".format(msg.data))
elif msg.tp == web.MsgType.close or\
msg.tp == web.MsgType.error:
break
recv_task = None
if tick_task in done:
ws.send_str("game loop ticks")
tick.release()
tick_task = None
return ws
async def game_loop():
while 1:
await tick.acquire()
tick.notify_all()
tick.release()
await asyncio.sleep(1)
asyncio.ensure_future(game_loop())
app = web.Application()
app.router.add_route('GET', '/connect', wshandler)
app.router.add_route('GET', '/', handle)
web.run_app(app)
(这个主要是asyncio.Condition的用法)
3.5 和线程一起使用
这个例子,我们把asyncio的loop放到另外一个单独线程中.上面也说过了,因为python的GIL的设计,不可能同时运行多个code.所以使用多线程来处理计算瓶颈的问题,并不是一个好主意.然后还有另外一个使用线程原因就是: 如果一些函数或者库不支持asyncio,那么就会阻塞住主线程的运行.这种情况下唯一的办法就是放在另外一个线程中.
要注意asyncio本身不是threadsafe的,可是提供了两个函数.call_soon_threadsafe和run_coroutine_threadsafe.
当你运行这个例子的时候,你会看到notify的线程id就是主线程的id,这是因为notify协程运行在主线程中,sleep运行在另外一个线程,所以不会阻塞住主线程.
import asyncio
from aiohttp import web
from concurrent.futures import ThreadPoolExecutor
import threading
from time import sleep
async def handle(request):
index = open("index.html", 'rb')
content = index.read()
return web.Response(body=content)
tick = asyncio.Condition()
async def wshandler(request):
ws = web.WebSocketResponse()
await ws.prepare(request)
recv_task = None
tick_task = None
while 1:
if not recv_task:
recv_task = asyncio.ensure_future(ws.receive())
if not tick_task:
await tick.acquire()
tick_task = asyncio.ensure_future(tick.wait())
done, pending = await asyncio.wait(
[recv_task,
tick_task],
return_when=asyncio.FIRST_COMPLETED)
if recv_task in done:
msg = recv_task.result()
if msg.tp == web.MsgType.text:
print("Got message %s" % msg.data)
ws.send_str("Pressed key code: {}".format(msg.data))
elif msg.tp == web.MsgType.close or\
msg.tp == web.MsgType.error:
break
recv_task = None
if tick_task in done:
ws.send_str("game loop ticks")
tick.release()
tick_task = None
return ws
def game_loop(asyncio_loop):
print("Game loop thread id {}".format(threading.get_ident()))
# a coroutine to run in main thread
async def notify():
print("Notify thread id {}".format(threading.get_ident()))
await tick.acquire()
tick.notify_all()
tick.release()
while 1:
task = asyncio.run_coroutine_threadsafe(notify(), asyncio_loop)
# blocking the thread
sleep(1)
# make sure the task has finished
task.result()
print("Main thread id {}".format(threading.get_ident()))
asyncio_loop = asyncio.get_event_loop()
executor = ThreadPoolExecutor(max_workers=1)
asyncio_loop.run_in_executor(executor, game_loop, asyncio_loop)
app = web.Application()
app.router.add_route('GET', '/connect', wshandler)
app.router.add_route('GET', '/', handle)
web.run_app(app)
3.6 多进程和扩展scaling up
一个线程的server可以工作了,但是这个server只有一个cpu可用. 为了扩展,我们需要运行多个进程,每个进程包含自己的eventloop. 所以我们需要进程之间通信的方式.同时在游戏领域,通常会有大量的计算(寻路什么的).这些任务通常不会很快完成(一个tick内). 在协程中进行大量耗时的计算没有意义,因为会阻塞住消息循环本身.所以在这种情况下,把大量的计算交给另外的进程就很有必要了
最简单的方式就是启动多个单线程的server.然后,可以使用haproxy这样的load balancer,来把客户端的连接分散到不同的进程上去.进城之间的通信有许多方法.一种是基于网络连接,也可以扩展到多个server.现在已经有很多实现了消息和存储系统的框架(基于asyncio). 比如:
aiomcache for memcached client
aiozmq for zeroMQ
aioredis for Redis storage and pub/sub
还有其他的一些乱七八糟,在git上,大部分是aio打头.
使用网络消息,可以非常有效的存储数据,或者交换信息.但是如果要处理大量实时的数据,而且有大量进程通信的情况,就不行了.在这种情况下,一个更合适的方法是使用标准的unix pipe.asyncio has support for pipes and there is a very low-level example of the server which uses pipes inaiohttp repository.
在这个例子中,我们使用python的高层次的multiprocessing库来触发一个新的进程来进行计算,通过multiprocessing.Queue来进行进程间通信.不幸的是,目前的multiprocessing实现并不支持asyncio.所以阻塞的调用就会阻塞住event loop. 这正是使用线程的最佳案例.因为我们在另外一个线程运行multiprocessing的代码.看代码
import asyncio
from aiohttp import web
from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor
from multiprocessing import Queue, Process
import os
from time import sleep
async def handle(request):
index = open("index.html", 'rb')
content = index.read()
return web.Response(body=content)
tick = asyncio.Condition()
async def wshandler(request):
ws = web.WebSocketResponse()
await ws.prepare(request)
recv_task = None
tick_task = None
while 1:
if not recv_task:
recv_task = asyncio.ensure_future(ws.receive())
if not tick_task:
await tick.acquire()
tick_task = asyncio.ensure_future(tick.wait())
done, pending = await asyncio.wait(
[recv_task,
tick_task],
return_when=asyncio.FIRST_COMPLETED)
if recv_task in done:
msg = recv_task.result()
if msg.tp == web.MsgType.text:
print("Got message %s" % msg.data)
ws.send_str("Pressed key code: {}".format(msg.data))
elif msg.tp == web.MsgType.close or\
msg.tp == web.MsgType.error:
break
recv_task = None
if tick_task in done:
ws.send_str("game loop ticks")
tick.release()
tick_task = None
return ws
def game_loop(asyncio_loop):
# coroutine to run in main thread
async def notify():
await tick.acquire()
tick.notify_all()
tick.release()
queue = Queue()
# function to run in a different process
def worker():
while 1:
print("doing heavy calculation in process {}".format(os.getpid()))
sleep(1)
queue.put("calculation result")
Process(target=worker).start()
while 1:
# blocks this thread but not main thread with event loop
result = queue.get()
print("getting {} in process {}".format(result, os.getpid()))
task = asyncio.run_coroutine_threadsafe(notify(), asyncio_loop)
task.result()
asyncio_loop = asyncio.get_event_loop()
executor = ThreadPoolExecutor(max_workers=1)
asyncio_loop.run_in_executor(executor, game_loop, asyncio_loop)
app = web.Application()
app.router.add_route('GET', '/connect', wshandler)
app.router.add_route('GET', '/', handle)
web.run_app(app)
worker()在另外一个进程中运行.包含了一些耗时计算,把结果放在queue中.得到结果之后,通知主线程的主eventloop那些等待的client.这个例子非常简陋,进程没有适当结束,同时worker可能需要另外一个queue来输入数据.
Important! If you are going to run anotherasyncio
event loop in a different thread or sub-process created from main thread/process, you need to create a loop explicitly, using asyncio.new_event_loop()
, otherwise, it will not work.