Why do a variable gets overridden in some threads?

55 views Asked by At

I am getting a variable overridden some times(Each time I run the script I get different result) in other threads, or if I split the total threads to run in chunks. Would you explain to me what does cause it to be overridden?

<?php

    require_once __DIR__ . '/vendor/autoload.php';
    require_once 'src/counties.php';
    require_once 'src/core.php';


    use \ByJG\PHPThread\ThreadPool as ScrapperPool;
    use RfcScrapper\Service\ParseAccounts;

    header("Content-type: application/json; charset=utf-8");

    $_POST['get-data'] = 1;
    $_POST['startDate'] = '04/01/2022';
    $_POST['endDate'] = '04/30/2022';
    $_POST['restart'] = 'yes';
    $_POST['force-run'] = 'no';
     
    $exe_status['start'] = $_POST['startDate'];
    $exe_status['end'] = $_POST['endDate'];
    try {
        $proxy = [];
        $credential = [];
        $start_date = isset($_POST['startDate']) ? $_POST['startDate'] : date('m/d/Y', time());
        $end_date = isset($_POST['endDate']) ? $_POST['endDate'] : date('m/d/Y', time());
        $proxies_list = ParseAccounts::create()->loadFile($config['accounts']['proxy'])->setAccountsType(ParseAccounts::ACCOUNTS_TYPE_PROXY)->parse();
        $login_list = ParseAccounts::create()->loadFile($config['accounts']['credential'])->setAccountsType(ParseAccounts::ACCOUNTS_TYPE_CREDENTIALS)->parse();
        $counties_chunks = array_chunk($counties, 3);
        foreach($counties_chunks as $county_chunk) {
            $proxy = getRandomArray($proxies_list, $proxy);
            $credential = getRandomArray($login_list, $credential);

            $scrapper_pool = new ScrapperPool();
            foreach($county_chunk as $county) {
                if($county['status'] === 'Available' || $county['status'] === 'Both') {
                    $rfc_scrapper = new ScrapperThread($county['endpoint']);
                    $rfc_scrapper->setDebugMode(true);
                    $rfc_scrapper->setDateRange($_POST['startDate'], $_POST['endDate']);
                    $rfc_scrapper->setCredential($credential['username'], $credential['password']);
                    $rfc_scrapper->setProxy($proxy['type'], $proxy['host'], $proxy['port'], $proxy['user'], $proxy['pass'] );
                    $scrap = function(ScrapperThread $instance) { 
                        return $instance->scrap(); 
                    };
                    echo '=======================================================' . PHP_EOL;
                    echo($county['endpoint']) . PHP_EOL;
                    echo('credential: ') . PHP_EOL; 
                    print_r($credential);   // <========================== Some times it gets override by $proxy var and have the same value with it!
                    echo('proxy: ') . PHP_EOL; 
                    print_r($proxy);
                    echo '=======================================================' . PHP_EOL;
                    $scrapper_pool->queueWorker($scrap, [$rfc_scrapper]);
                }
            }

            $scrapper_pool->startPool();
            $scrapper_pool->waitWorkers();
    
            // Get results
            foreach($scrapper_pool->getThreads() as $county_worker) {
                print_r($scrapper_pool->getThreadResult($county_worker));
                echo PHP_EOL . '=======================================================' . PHP_EOL;
            }

            $proxy = [];
            $credential = [];
        }
    }
    catch(Exception $e) {
        echo $e->getMessage();
    }






    function getRandomArray(array $arr, array $unique) {
        $result = $arr[array_rand( $arr, 1 )];
        if(isEqualArray($result, $unique)) {
            return getRandomArray($arr, $unique);
        }

        return $result;
    }


    function isEqualArray($array1, $array2)
    {
        array_multisort($array1);
        array_multisort($array2);
        return (serialize($array1) === serialize($array2));
    }

    
    
    class ScrapperThread {
        private $endpoint = '';
        private $start_date = '';
        private $end_date = '';

        private $proxy_type = '';
        private $proxy_host = '';
        private $proxy_port = '';
        private $proxy_user = '';
        private $proxy_password = '';

        private $user = '';
        private $password = '';


        public function __construct($endpoint_name = '') {
            $this->endpoint = $endpoint_name;
        }

        public function setEndPoint($name) {
            $this->endpoint = $name;
        }

        public function setDateRange($start, $end) {
            $this->start_date = $start;
            $this->end_date = $end;
        }

        public function setCredential($user, $password) {
            $this->user = $user;
            $this->password = $password;
        }

        public function setProxy($type, $host, $port, $user, $password) {
            $this->proxy_type = $type;
            $this->proxy_host = $host;
            $this->proxy_port = $port;
            $this->proxy_user = $user;
            $this->proxy_password = $password;
        }

        public function setDebugMode($mode) {
            $this->enableDebug = $mode;
        }


        public function getAllSetters() {
            return [
                'endpoint' => $this->endpoint,

                'start_date' => $this->start_date,
                'end_date' => $this->end_date,

                'user' => $this->user,
                'password' => $this->password,

                'proxy_type' => $this->proxy_type,
                'proxy_host' => $this->proxy_host,
                'proxy_port' => $this->proxy_port,
                'proxy_user' => $this->proxy_user,
                'proxy_password' => $this->proxy_password,
            ];
        }

        public function scrap() {
            echo 'This is a scrapping thread of ' . $this->endpoint . ' county.' . PHP_EOL;
            //print_r(self::getAllSetters());
            echo '=======================================================' . PHP_EOL;
        }
    }

?>

Output:

Notice: Undefined index: username in /home/forefgih/public_html/realforeclose-data-scraper-V2.0/testthread.php on line 40

Notice: Undefined index: password in /home/forefgih/public_html/realforeclose-data-scraper-V2.0/testthread.php on line 40
=======================================================
indian-river
credential:                                  // This one gets override and some times not
Array
(
    [type] => SOCKS5
    [host] => 149.12.24.99
    [port] => 8888
    [user] => UserProxy
    [pass] => PassProxy
)
proxy:
Array
(
    [type] => SOCKS5
    [host] => 149.12.24.99
    [port] => 8888
    [user] => UserProxy
    [pass] => PassProxy
)
=======================================================

Notice: Undefined index: username in /home/forefgih/public_html/realforeclose-data-scraper-V2.0/testthread.php on line 40

Notice: Undefined index: password in /home/forefgih/public_html/realforeclose-data-scraper-V2.0/testthread.php on line 40
=======================================================
putnam
credential:                                  // This one gets override and some times not
Array
(
    [type] => SOCKS5
    [host] => 149.12.24.99
    [port] => 8888
    [user] => UserProxy
    [pass] => PassProxy
)
proxy:
Array
(
    [type] => SOCKS5
    [host] => 149.12.24.99
    [port] => 8888
    [user] => UserProxy
    [pass] => PassProxy
)
=======================================================

Notice: Undefined index: username in /home/forefgih/public_html/realforeclose-data-scraper-V2.0/testthread.php on line 40

Notice: Undefined index: password in /home/forefgih/public_html/realforeclose-data-scraper-V2.0/testthread.php on line 40
=======================================================
okeechobee
credential:                                  // This one gets override and some times not
Array
(
    [type] => SOCKS5
    [host] => 149.12.24.99
    [port] => 8888
    [user] => UserProxy
    [pass] => PassProxy
)
proxy:
Array
(
    [type] => SOCKS5
    [host] => 149.12.24.99
    [port] => 8888
    [user] => UserProxy
    [pass] => PassProxy
)
=======================================================
This is a scrapping thread of okeechobee county.
=======================================================
This is a scrapping thread of putnam county.
=======================================================
This is a scrapping thread of indian-river county.
=======================================================

=======================================================

=======================================================

=======================================================

Notice: Undefined index: username in /home/forefgih/public_html/realforeclose-data-scraper-V2.0/testthread.php on line 40

Notice: Undefined index: password in /home/forefgih/public_html/realforeclose-data-scraper-V2.0/testthread.php on line 40
=======================================================
walton
credential:                                  // This one gets override and some times not
Array
(
    [type] => SOCKS5
    [host] => 149.12.24.99
    [port] => 8888
    [user] => UserProxy
    [pass] => PassProxy
)
proxy:
Array
(
    [type] => SOCKS5
    [host] => 149.12.24.99
    [port] => 8888
    [user] => UserProxy
    [pass] => PassProxy
)
=======================================================

Notice: Undefined index: username in /home/forefgih/public_html/realforeclose-data-scraper-V2.0/testthread.php on line 40

Notice: Undefined index: password in /home/forefgih/public_html/realforeclose-data-scraper-V2.0/testthread.php on line 40
=======================================================
nassau
credential:                                  // This one gets override and some times not
Array
(
    [type] => SOCKS5
    [host] => 149.12.24.99
    [port] => 8888
    [user] => UserProxy
    [pass] => PassProxy
)
proxy:
Array
(
    [type] => SOCKS5
    [host] => 149.12.24.99
    [port] => 8888
    [user] => UserProxy
    [pass] => PassProxy
)
=======================================================

Notice: Undefined index: username in /home/forefgih/public_html/realforeclose-data-scraper-V2.0/testthread.php on line 40

Notice: Undefined index: password in /home/forefgih/public_html/realforeclose-data-scraper-V2.0/testthread.php on line 40
=======================================================
citrus
credential:                                  // This one gets override and some times not
Array
(
    [type] => SOCKS5
    [host] => 149.12.24.99
    [port] => 8888
    [user] => UserProxy
    [pass] => PassProxy
)
proxy:
Array
(
    [type] => SOCKS5
    [host] => 149.12.24.99
    [port] => 8888
    [user] => UserProxy
    [pass] => PassProxy
)
=======================================================
This is a scrapping thread of walton county.
=======================================================
This is a scrapping thread of citrus county.
=======================================================
This is a scrapping thread of nassau county.
=======================================================

=======================================================

=======================================================

=======================================================

Notice: Undefined index: username in /home/forefgih/public_html/realforeclose-data-scraper-V2.0/testthread.php on line 40

Notice: Undefined index: password in /home/forefgih/public_html/realforeclose-data-scraper-V2.0/testthread.php on line 40
=======================================================
martin
credential:                                  // This one gets override and some times not
Array
(
    [type] => SOCKS5
    [host] => 149.12.24.99
    [port] => 8888
    [user] => UserProxy
    [pass] => PassProxy
)
proxy:
Array
(
    [type] => SOCKS5
    [host] => 149.12.24.99
    [port] => 8888
    [user] => UserProxy
    [pass] => PassProxy
)
=======================================================
This is a scrapping thread of martin county.
=======================================================

=======================================================
0

There are 0 answers