Coroutine em python entre 3.4 e 3.5, Como posso manter a compatibilidade com palavras cruzadas?
Estou desenvolvendo a estrutura de bot de bate-papo em python comasyncio
. Mas eu assistoPEP-492 e há uma nova sintaxe,async
/await
e finalmente aceitou.
Eu gostoasync
/await
sintaxe e eu quero usá-lo. mas eu me preocupo com a compatibilidade com 3,4 palavras-chave.
Se eu usar uma nova sintaxe no meu código, alguém poderá usá-lo no 3.4?
Por exemplo, escrevo um código como este,
import asyncio
class ChatBot:
def __init__(self, loop):
self.loop = loop
async def connect(self):
self.reader, self.writer = await asyncio.open_connect(HOST, PORT, loop=self.loop)
async def read():
return await self.reader.read()
async def run(self):
running = True
while running:
try:
await self.connect()
line = await self.read()
if not line:
continue
await self.parse(line)
except BotInternalError as e:
if e.stop:
running = False
break
except:
pass
async def parse(self, msg):
if msg.startswith('PING'):
self.pong()
elif msg.startswith('ERROR'):
self.error()
else:
await self.some_work(msg)
async def some_work(self, msg):
# some looooooooong works
self.send(msg)
def send(self, msg):
self.writer.write(msg)
Então, eu posso usá-lo com esta fonte em py35
loop = asyncio.get_event_loop() # I don't know it really needed in py35.
bot = ChatBot(loop)
asyncio.run_until_complete(bot.run())
Mas, py34 não temawait
sintaxe. Se eu fiz o upload acima da fonte no PyPI sem restrição de versão e alguém o instalou no py34, funcionará bem? Como posso mantê-lo?