multithreading - IP switching over PHP crawler -
i looking solution switch ips automatically when running php crawler. have customized crawler runs 100 threads due throttle limits blocked. since php doesn't support multi-threading have set windows scheduler run php application in parallel.
i assign each thread different ip address , welcome suggestion overcome issue.
multi-threading possible in php
some might call trivial ...
<?php define('log', mutex::create()); /* make output when writing stdout thread safe (so, readable) */ function slog($message, $args = []) { $args = func_get_args(); if (($message = array_shift($args))) { mutex::lock(log); echo vsprintf($message, $args); mutex::unlock(log); } } class webcrawler extends thread { public function __construct($interface) { $this->interface = $interface; } public function run() { slog("thread %lu using %s\n", $this->getthreadid(), $this->getinterface()); } public function getinterface() { return $this->interface; } protected $interface; } $interfaces = [ "192.168.0.1", "192.168.0.2", "192.168.0.3", "192.168.0.4", "192.168.0.5" ]; $threads = []; $thread = 0; while (count($threads) < count($interfaces)) { $threads[$thread] = new webcrawler($interfaces[$thread]); $threads[$thread]->start(); $thread++; } foreach ($threads $thread) $thread->join(); mutex::destroy(log); ?> the code above gives interface per thread pre-defined list of interfaces. configure clients use interface set thread using curlopt_interface or other magic.
further reading:
- http://php.net/pthreads
- https://gist.github.com/krakjoe/6437782 <- always read this
- https://gist.github.com/krakjoe/9384409 <- probably helpful specific task
Comments
Post a Comment