Hi,
What is the right way to share common variables/objects between Threads that are auto created for each new connection?
I do not understand what you mean. The global variables and objects can be accessed anywhere
Well, yes tried the global variables...
for example i declared a global variable called $test = [];
afterall i wanted to use between each new Thread created by Swoole...having a common variable where each of the threads can Read/Write (with Locks) having the same memory stack... (same pointer) => but as i tested it didn't had same memory... created multiple Client connections to the server and was printing the Data out...each of them was having each own memory stack... (looking by htop from bash they are also separated as subprocesses wich is normal but no luck with Global Vars...)
So don't know what i'm doing Wrong...because i already worked multiple times with \Worker (pthreads) and i always shared common Stacks...
Thinking to use redis, but still that's just a secondary solution :) so still thinking what's Wrong in global/GLOBALS vars... maybe when Swoole is cloning itself it creates different memory stacks?
Is it possible to set some Variables to swoole_server where therefore all threads will have access to them?
Thank you for your time!
An example:
$ws_server = new \swoole_websocket_server("0.0.0.0", 8085);
$GLOBALS['test_data'] = [];
$ws_server->on('open', function (\swoole_websocket_server $server, \swoole_http_request $req) {
// global $test_data;
// global $test_data;
$GLOBALS['test_data'][] = rand(5000, 999999);
print_r($GLOBALS['test_data']);
// $rand_key = rand(5000, 9999999);
// $swoole_table->
// print_r($swoole_table->);
});
$ws_server->on('message', function (\swoole_websocket_server $server, \swoole_websocket_frame $frame) {
});
$ws_server->on('close', function (\swoole_websocket_server $server, $fd) {
});
$ws_server->start();
Second example:
$ws_server = new \swoole_websocket_server("0.0.0.0", 8085);
$ws_server->test_data = [];
$ws_server->on('open', function (\swoole_websocket_server $server, \swoole_http_request $req) {
// global $test_data;
// global $test_data;
$server->test_data[] = rand(5000, 999999);
print_r($server->test_data);
// $rand_key = rand(5000, 9999999);
// $swoole_table->
// print_r($swoole_table->);
});
$ws_server->on('message', function (\swoole_websocket_server $server, \swoole_websocket_frame $frame) {
});
$ws_server->on('close', function (\swoole_websocket_server $server, $fd) {
});
$ws_server->start();
Third example:
$test_data = [];
$ws_server = new \swoole_websocket_server("0.0.0.0", 8085);
$ws_server->on('open', function (\swoole_websocket_server $server, \swoole_http_request $req) use ($test_data) {
// global $test_data;
$test_data[] = rand(5000, 999999);
print_r($test_data);
$rand_key = rand(5000, 9999999);
// $swoole_table->
// print_r($swoole_table->);
});
$ws_server->on('message', function (\swoole_websocket_server $server, \swoole_websocket_frame $frame) {
});
$ws_server->on('close', function (\swoole_websocket_server $server, $fd) {
});
$ws_server->start();
and the fourth one is with global... which doesn't work also :(
SWOOLE_BASE (is this mode still creating separate threads? as i tested i suppose yes) :) if yes then this resolves my problem... (because it's 1 process) SWOOLE_PROCESS => no global variables..
PHP does not support multi-thread. Swoole uses a multi-process model. Different worker process cannot access other's memory.If you want to read and write data across processes, please use swoole_table or swoole_atomic
ok thank you, this should be closed then!
@matyhtf i am confused, PHP supports multi-thread through it's tsrm and two extensions are available in the pecl for userspace threads with varying success even if not fully comparable to thread features of other languages (see PHP extensions pthreads and pht). Additionally swoole has other IPC mechanisms that it states are useful between threads
https://www.swoole.co.uk/docs/modules/swoole-table says table object is safe between threads and processes.