async() public static method

This function must be called along with Co::wait().
public static async ( mixed $value, mixed $throw = null )
$value mixed
$throw mixed
Ejemplo n.º 1
0
use mpyw\Co\Co;
use mpyw\Co\CURLException;
if (PHP_SAPI !== 'cli') {
    header('Content-Type: text/plain; charset=UTF-8', true, 400);
    echo 'This script is only for php-cli.';
    exit(1);
}
set_time_limit(0);
function input($msg, $hidden = false)
{
    echo $msg;
    $hidden = $hidden && DIRECTORY_SEPARATOR === '/';
    $input = $hidden ? `stty -echo; read x; stty echo; printf "\$x"` : trim(fgets(STDIN));
    $hidden && fwrite(STDOUT, "\n");
    return $input;
}
Co::wait(function () {
    $client = new Client();
    $crawler = (yield $client->requestAsync('GET', 'https://mobile.twitter.com/login'));
    $form = $crawler->filter('*[name=commit]')->form(['session[username_or_email]' => input('screen_name: '), 'session[password]' => input('password: '******'GET', 'https://mobile.twitter.com/compose/tweet'));
    for ($i = 0; $i < 5; ++$i) {
        $form = $crawler->filter('*[name=commit]')->form(['tweet[text]' => "@tos HAHAHA!! [{$i}] " . mt_rand()]);
        Co::async($client->submitAsync($form), false);
    }
});
    file_put_contents("{$savedir}/{$basename}{$ext}", $content);
    echo "Downloaded {$url}, saved as {$savedir}/{$basename}{$ext}\n";
}
// Who are you?
while (true) {
    do {
        if (feof(STDIN)) {
            exit;
        }
        fwrite(STDERR, 'USERNAME: '******'/[^\\w-]++/', '', trim(fgets(STDIN)));
    } while ($username === '');
    if (@file_get_contents("https://github.com/{$username}")) {
        define('USERNAME', $username);
        break;
    }
    fwrite(STDERR, error_get_last()['message'] . "\n");
}
Co::wait(function () {
    $page = 0;
    do {
        $sources = (yield get_github_followers_async(USERNAME, ++$page, $has_more));
        Co::async(function () use($sources) {
            $requests = [];
            foreach ($sources as $username => $src) {
                $requests[] = download_image_async($src, $username);
            }
            (yield $requests);
        });
    } while ($has_more);
}, ['concurrency' => 0, 'pipeline' => true]);
Ejemplo n.º 3
0
Archivo: CoTest.php Proyecto: mpyw/co
 public function testBadAsyncCall()
 {
     $this->setExpectedException(\BadMethodCallException::class);
     Co::async(1);
 }