G
G
grisha22172019-12-13 05:20:08
PHP
grisha2217, 2019-12-13 05:20:08

How to properly use asynchrony in MadelineProto Php?

Hello. I'm trying to write a script that checks the availability of telegram logins. To do this, there is the account.checkUsername method in the telegram api. I need to check 100+ logins at once, in single-threaded mode it takes more than a minute. Decided to rewrite on async.
I wrote this code, it works if you disable the asynchronous mode. I turn on the mode - it stops working. If you make var_dump inside foreach, then there will be a generator object.

$MadelineProto->async(true);
$me = $MadelineProto->get_self();
$MadelineProto->logger($me);

function handleUsers($usernames, $MadelineProto)
{
  $i = 0;
  $count = count($usernames);
  while ($i < $count)
  {
    yield $usernames[$i]['field_value'] => $MadelineProto->account->checkUsername(['username' => $usernames[$i]['field_value']]);
    $i++;
  }
}

foreach (handleUsers($usernames, $MadelineProto) AS $login => $available)
{
  if ($available)
  {
    // Available
  }
  else
  {
    // Not available
  }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
Lev Zabudkin, 2020-01-17
@zabudkin

yield $usernames[$i]['field_value'] => $MadelineProto->account->contacts.search(['username' => $usernames[$i]['field_value']],1);
    $i++;

Maybe it's better like this?
Perhaps I made a mistake in the writing above, but in fact, contacts.search is better than checking each user. And as far as I know, and even if the user is renamed, then in contacts.search you will find him by the old name, but in checkUsername only by the name that is now.

X
xmoonlight, 2020-01-17
@xmoonlight

It's possible that a race-condition or "race condition" is happening there.
Try to get rid of yield and assign all intermediate values ​​through variables.
The easiest way to check: two checks in a row without a loop with intermediate variables and immediately after each - var_dump of all variables.
Remote server response codes - it is also desirable to receive.
In extreme cases - a sniffer and packet analysis: perhaps a limit on parallel connections is set on the remote server.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question