R
R
roginvs2016-02-20 11:03:01
Asynchronous programming
roginvs, 2016-02-20 11:03:01

Generating async code from normal, if there is a way?

I want, for example, to write a WS server. Keeping a process per connection is not very good, so things like this should be done asynchronously. Writing asynchronous code is a bit more tedious than regular linear code, and I want to be able to write code as usual, but for the preprocessor to already convert it to async.

# Т.е. обычно, к примеру, пишу
my $result = ask_database('query');

# когда тоже самое, но асинхронно:
ask_database('query', sub {
  my $result = shift;
});

Because the code is locked mainly on waiting for a response from the network (from the database, or from another API), then why not do such a code transformation automatically? After all, we know when the code will call a function on which we can block.
Or another option:
// Из этого
console.info('test1');
hypothetic_sleep(1000);
console.info('test2');

// Автоматически бы делалось это
console.info('test1');
setTimeout(function() {
  console.info('test2');
})

In python, tornado has gen.coroutine , which seems to be trying to do just such a simplification, but the problem is that not all libraries support tornado and async calls.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
roginvs, 2018-07-02
@roginvs

nodejs + async/await

R
redakoc, 2016-02-20
@redakoc

Go can. Minimal body movements.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question