Answer the question
In order to leave comments, you need to log in
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
yield $usernames[$i]['field_value'] => $MadelineProto->account->contacts.search(['username' => $usernames[$i]['field_value']],1);
$i++;
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 questionAsk a Question
731 491 924 answers to any question