Avatar ·

asyncio: correct stopping of other coroutines

📁 Advanced Python Topics, question, Error, example, python3, asyncio, web, AI, game

I'm back with the same problem. In [previous context] all tasks were under my control, and I could manually close them. But here's the trouble: I needed websockets. In the examples in [documentation], no proper shutdown is performed:

start_server = websockets.serve(hello, 'localhost', 8765)asyncio.get_event_loop().run_until_complete(start_server)asyncio.get_event_loop().run_forever()

I played around with the examples, and yes, when pressing Ctrl+C it throws an error if there is a connected client:

$ python websocket_server.py ^CKeyboardInterruptException ignored in: Task was destroyed but it is pending!task: <Task pending coro=<run() runnin                    
Avatar ·

Try the script first on your own page (specify OWNER_ID = None). Then on another user's page. As far as I remember, VK groups have OWNER_ID different from users – their id is written with a minus sign:

import vk_apiimport sysimport random# Script posts images to the user's vk.com walldef vk_auth(login, password):    vk = vk_api.VkApi(login, password)    try:        vk.authorization()  # Authorize    except vk_api.AuthorizationError as error_msg:        print(error_msg)  # Print error message if any        sys.exit()    retu vk# Login, password for the account and id                                

Log in to leave an answer

Blogs