|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2014-12-10 08:30 UTC] hewei986532 at sina dot com
Description:
------------
It throw a error when execute a PDO's instance in the 'run' of Pthreads's method.
Fatal error: Uncaught exception 'PDOException' with message 'You cannot serialize or unserialize PDO instances'
Test script:
---------------
class Async extends Thread
{
/**
* Provide a passthrough to call_user_func_array
* */
public function __construct($method, $params)
{
$this->method = $method;
$this->params = $params;
$this->result = null;
$this->joined = false;
}
/**
* The smallest thread in the world
* */
public function run()
{
$this->result=call_user_func_array($this->method, $this->params);
}
/**
* Static method to create your threads from functions ...
* */
public static function call($method, $params)
{
$thread = new Async($method, $params);
if ($thread->start()) {
return $thread;
} /** else throw Nastyness * */
}
}
class test
{
public $dbh;
public function __construct()
{
$dsn = 'mysql:dbname=mysql;host=127.0.0.1';
$user = 'root';
$password = '123456';
$this->dbh = new PDO($dsn, $user, $password);
}
public function world()
{
$sql = 'SELECT * FROM user ';
foreach ($this->dbh->query($sql) as $row) {
print $row['User'] . "\t";
}
}
}
$cc = new test;
/* here's us calling file_get_contents in a thread of it's own */
$future = Async::call(array($cc, 'world'), array());
Expected result:
----------------
error
Actual result:
--------------
no error
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Oct 24 09:00:01 2025 UTC |
That's not a bug. Arrays and objects that don't extend Threaded are stored serialized. Example: <?php class Test extends Threaded { public function __sleep() { echo "No! I'm not tired!\n"; return []; } } $thread = new Thread; $thread->test = new Test; $thread->start(); // prints "No! I'm not tired!" ?> This is also explained in the introduction: http://php.net/manual/intro.pthreadsMy last comment actually has a bug on its own. Here's the fixed example: <?php class Test { public function __sleep() { echo "No! I'm not tired!\n"; return []; } } $thread = new Thread; $thread->test = new Test; $thread->start(); // prints "No! I'm not tired!" ?>