Continuing the series on Python 3.4’s asyncio module, we will see how to create a simple text-based game. The goal of the game is to display a maze and let the player move around using the numeric keypad (4 - left, 6 - right, 8 - up, 2 - down, and S for exit). To exercise our old DOS programming muscles with asynchronous programming, we will display the clock on the last line. The final result should look like this image:
Posts for: #Python 3
Asyncio - Reading from the Keyboard
Continuing the series on Python 3.4’s asyncio module, we will see how to create a simple text-based game. The goal of the game is to display a maze and let the player move around using the numeric keypad (4 - left, 6 - right, 8 - up, 2 - down, and S for exit). To exercise our old DOS programming muscles with asynchronous programming, we will display the clock on the last line. The final result should look like this image:
Asyncio e Coroutines
Continuando a série sobre o módulo asyncio do Python 3.4, vou apresentar as corotinas e como elas simplificam a escrita de nossos programas com o loop de eventos.
Modificamos também a chamada de execução da corotina, pois agora utilizamos loop.run_until_complete para iniciar nossa corotina principal. Aproveitamos para colocar tudo entre um try-finally para terminar a execução do loop corretamente (mesmo em caso de exceção). Perceba que no exemplo anterior, com call_soon, passamos a função e seus parâmetros, mas não executamos a função em si. No caso de run_until_complete, estamos passando o retorno da chamada de print_and_repeat que é uma corotina, uma vez que a marcamos com o decorador @asyncio.coroutine.
Asyncio e Coroutines
Continuando a série sobre o módulo asyncio do Python 3.4, vou apresentar as corotinas e como elas simplificam a escrita de nossos programas com o loop de eventos.
Modificamos também a chamada de execução da corotina, pois agora utilizamos loop.run_until_complete para iniciar nossa corotina principal. Aproveitamos para colocar tudo entre um try-finally para terminar a execução do loop corretamente (mesmo em caso de exceção). Perceba que no exemplo anterior, com call_soon, passamos a função e seus parâmetros, mas não executamos a função em si. No caso de run_until_complete, estamos passando o retorno da chamada de print_and_repeat que é uma corotina, uma vez que a marcamos com o decorador @asyncio.coroutine.
Asyncio - Asynchronous Methods in Python
With the release of Python 3.4, I updated my book on Introduction to Programming with Python. Some topics fall outside the scope of the book, which is intended for beginners. I’m going to start writing a series of short posts about some interesting topics that I think are worth discussing and might even be the basis for a new book.
One of the new features in Python 3.4 is the asyncio module, which brings various routines for calling asynchronous methods in Python. Asynchronous programming is a bit different from what we’re used to writing in Python, but it’s an excellent alternative to using threads and a good choice for solving problems with many inputs or outputs (I/O).
Asyncio - Asynchronous Methods in Python
With the release of Python 3.4, I updated my book on Introduction to Programming with Python. Some topics fall outside the scope of the book, which is intended for beginners. I’m going to start writing a series of short posts about some interesting topics that I think are worth discussing and might even be the basis for a new book.
One of the new features in Python 3.4 is the asyncio module, which brings various routines for calling asynchronous methods in Python. Asynchronous programming is a bit different from what we’re used to writing in Python, but it’s an excellent alternative to using threads and a good choice for solving problems with many inputs or outputs (I/O).