M
M
Maxim Alyukov2020-06-16 17:58:56
Python
Maxim Alyukov, 2020-06-16 17:58:56

How to asynchronously run a function that blocks control?

Hello

Liba in the example - python-a2s

Task:
- Write a function that asynchronously starts the game server parser.
Problem:
The parser has a timeout that blocks control. That is, the code is not executed until a response from the game server arrives.
Question:
How to run the parser for several servers and not stumble over the timeout of one server?

# Пример парсера одного сервера
def parse_of_server(server_address):
    try:
        server_info = a2s.info(server_address)
    except:
        server_info = None
    
    return server_info


PS I'm struggling with asyncio. If there are other options, then you are welcome.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dimonchik, 2020-06-16
@mvxmvl

1) find any asynchronous parser code (asyncio, loop - that's all )
2) replace calls with calls to your function (operation is not guaranteed)
discard
read about multithreading and multiprocessing

D
Dr. Bacon, 2020-06-16
@bacon

1. And where is the code with asyncio?
2. Without asyncio, run in a thread/process and it's better to use something high-level, like Executor https://docs.python.org/3/library/concurrent.futur...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question