I have installed fresh xampp (7.0.2 atm). I've created php-cli.ini, added pthread extension there and set memory limit to 3 gb. But when I'am trying to launch thread script I got this:
PHP Fatal error: Uncaught RuntimeException: cannot start my_thread, out of reso
urces in C:\xampp\htdocs\w\start_threads.php:160
Stack trace:
#0 C:\xampp\htdocs\w\start_threads.php(160): Thread->start()
#1 {main}
thrown in C:\xampp\htdocs\w\start_threads.php on line 160
Fatal error: Uncaught RuntimeException: cannot start my_thread, out of resources
in C:\xampp\htdocs\w\start_threads.php:160
(I'am using pthreds 3.1.5 x86)
What am I doing wrong here? Thank you!
Essentially, this is caused by pthread_create returning EAGAIN: It means that the system lacks resources to create another thread, or that the system imposed limit on the maximum number of threads (in a process, or system wide) has been reached.
This can be caused by two things, the purposeful use of more threads than a process can handle simultaneously as a result of the way some software is designed, or more perniciously, as a result of less than graceful joining of threads.
If you only seem to hit such errors sometimes, it would suggest the latter is going on; Be sure to cleanup (explicitly join) threads you are done with to make behaviour predictable.
My PHP version: 7.2.6 x82
And pthreads: php_pthreads-3.1.6-7.2-ts-vc15-x86
I created 25 threads, when created 21th thread then occurred same error.
I thought that it only can create 20 threads.
So I edited my code and that error does not occur
My code:
class ReadAllFile extends Thread {
public $folder;
public function __construct($folder) {
$this->folder = $folder;
}
public function run() {
//code process
}
}
$dir = "F:/sbd/sbdstore/20180606/";
$subFolders = scandir ( $dir );
$stack = array();
foreach ( $subFolders as $folder ) {
if ($folder != '.' && $folder != '..') {
$stack[] = new ReadAllFile ( $dir.$folder );
}
}
$maxNumberOfThread = 20;
$numberOfRunning = 0;
$numberOfStack = count($stack);
$elementIsStarted = array();
$allElementIsProcess = false;
while(count($stack)){
if($numberOfRunning <= $maxNumberOfThread && !$allElementIsProcess){
for($i=0;$i<$numberOfStack;$i++){
if(!in_array($i,$elementIsStarted)){
$numberOfRunning++;
$elementIsStarted[] = $i;
$stack[$i]->start();
if($i == $numberOfStack - 1){
$allElementIsProcess = true;
}
$i = $numberOfStack + 1;
}
}
}else{
foreach($elementIsStarted AS $element){
if(isset($stack[$element]) && $stack[$element]->isRunning() !== true){
unset($stack[$element]);
$numberOfRunning--;
}
}
}
}
Hope this help.
Sorry about my English.
P/s: If I use PHP version: 7.2.6 x64 and php_pthreads-3.1.6-7.2-ts-vc15-x64 then it does not occur this error.
I think x64 allocation more memory.
Related
I running magento2 using bitnami docker containers and I installed claue theme. After installing claue theme I am getting error while loading the landing page(home page).
Error:
[php7:error] [pid 524] [client 172.18.0.1:36096] PHP Fatal error: Declaration of MGS\\Mpanel\\Helper\\Swatches\\Data::getProductMediaGallery(Magento\\Catalog\\Model\\Product $product) must be compatible with Magento\\Swatches\\Helper\\Data::getProductMediaGallery(Magento\\Catalog\\Model\\Product $product): array in /bitnami/magento/htdocs/app/code/MGS/Mpanel/Helper/Swatches/Data.php on line 0
Swatches/Data.php
use Magento\Catalog\Model\Product as ModelProduct;
public function getProductMediaGallery(ModelProduct $product){
if(!in_array($product->getData('image'), [null, self::EMPTY_IMAGE_VALUE], true)){
$baseImage = $prodcut->getData('image');
}else{
$productMedaiAttributes = array_filter($product->getMediaAttributeValues(), function($value){
return $value !== self::EMPTY_IMAGE_VALUE && $values !== null;
});
foreach ($productMediaAttributes as $attributeCode => $value){
if($attributeCode !== 'swatch_image'){
$baseImage = (string)$value;
break;
}
}
}
if(empty($baseImage)){
return [];
}
$resultGallery = $this->getAllSizeImages($product, $baseImage);
$resultGallery['gallery'] = $this->getGalleryImages($product);
return $resultGallery;
}
I am new to php and magento. I am not able to find out how to solve this issue. kindly help to solve this issue. If any details is need kindly let me know.
Thanks in advance.
As the error message says you have to make your method compatible with the one you overrided by adding the return type, (in this case : array) at the end of your method declaration.
This line of your code : public function getProductMediaGallery(ModelProduct $product){ will become : public function getProductMediaGallery(ModelProduct $product): array {
I have a legacy site (not written by me) that has been on a server with php5 on it for the last several years. I am in the process of creating a new server with php7 on it and testing what works and is broken.
the site uses pear by including the file pear/lib/DB.php. i created a brand new page that only has the code
<?php
require_once( "DB.php" );
?>
this presents the exact same error as the full site.
the error that's being presented is
PHP Parse error: syntax error, unexpected 'new' (T_NEW) in /local/sites/php/pear/lib/DB.php on line 310
the site only requires DB.php because I have added Pear to the php.ini in include_path
checking the version of Pear gives me the following
$ pear version
PEAR Version: 1.10.3
PHP Version: 7.0.15-0ubuntu0.16.04.4
Zend Engine Version: 3.0.0
Running on: Linux cdc-migration-0d 3.13.0-103-generic #150-Ubuntu SMP Thu Nov 24 10:34:17 UTC 2016 x86_64
from my research it shows the latest version of Pear is php7 compatible, so these should work together. any idea why just requiring the DB.php on a test page would immediately generate the parsing error?
edit:
the code in the pear file that is generating the error is as follows
function &factory($type, $options = false)
{
if (!is_array($options)) {
$options = array('persistent' => $options);
}
if (isset($options['debug']) && $options['debug'] >= 2) {
// expose php errors with sufficient debug level
include_once "DB/{$type}.php";
} else {
#include_once "DB/{$type}.php";
}
$classname = "DB_${type}";
if (!class_exists($classname)) {
$tmp = PEAR::raiseError(null, DB_ERROR_NOT_FOUND, null, null,
"Unable to include the DB/{$type}.php file",
'DB_Error', true);
return $tmp;
}
#$obj =& new $classname; // ##### this is line 310 that generates the error #####
foreach ($options as $option => $value) {
$test = $obj->setOption($option, $value);
if (DB::isError($test)) {
return $test;
}
}
return $obj;
}
#$obj =& new $classname;
Assigning the return value of new by reference is deprecated since PHP 5.3. http://php.net/manual/en/migration53.deprecated.php
This is PHP4 style of writing PHP.
Write instead :
$obj = new $classname;
This has been removed as of PHP7.
See: http://php.net/manual/en/migration70.incompatible.php#migration70.incompatible.other.new-by-ref
I'm using phpfastcache https://github.com/khoaofgod/phpfastcache/
when I try to delete the cache I get an error
Warning: unlink(C:\...//sqlite/indexing): Permission denied in C:\...drivers\sqlite.php on line 328
I usually see that kind of error when there is a process not releasing the handle of those files.
Step to reproduce
// Require phpfastcache
require_once 'phpfastcache_v2.1_release\phpfastcache\phpfastcache.php';
// Simple singleton
class MyCache extends phpFastCache
{
private static $istance;
Private $obCache;
function __construct()
{
$option = array('securityKey' => 'aCache', 'path' => dirname(__FILE__));
$this->obCache = parent::__construct('sqlite', $option);
}
public static function getIstance()
{
if( is_null(self::$istance) )
{
self::$istance = new self();
}
return self::$istance;
}
}
// check if cached
if( $CacheData = MyCache::getIstance()->get('aKeyword') )
{
die('Cached');
}
// store in cache
MyCache::getIstance()->set('aKeyword','aValue', 60*60*24);
// clean cache (throw error)
MyCache::getIstance()->clean();
die('No cached');
this is the method of "phpfastcache" that generates the error
function driver_clean($option = array()) {
// delete everything before reset indexing
$dir = opendir($this->path);
while($file = readdir($dir)) {
if($file != "." && $file!="..") {
unlink($this->path."/".$file);
}
}
}
does anyone know how to fix this?
I'm temporarily using #unlink()
I tried but nothing has changed
chmod($this->path."/".$file, 0777);
unlink($this->path."/".$file);
UPDATE
I'm under windows...
UPDATE 2
I installed XAMPP using the admin account, after installation run with admin privileges...
UPDATE 3
Solution:
function driver_clean($option = array()) {
// close connection
$this->instant = array();
$this->indexing = NULL;
// delete everything before reset indexing
$dir = opendir($this->path);
while($file = readdir($dir)) {
if($file != "." && $file!="..") {
unlink($this->path."/".$file);
}
}
}
The solution depends on the environment that serves the script.
If it's CLI, the ability to creating, deleting or modifing files are controlled by the executing user.
If it's a PHP Stack ( WAMP, XAMPP, ZendServer or own Webserver+PHP+MySQL-Stack ) the executing layer ( apache, nginx ) must use an user which has rights to do what you want to do.
In both cases it depends on what you've configured or what had been inherited to your script, directory or drive.
Permission Knowledge could be found here: http://technet.microsoft.com/en-us/library/cc770962.aspx
(Doesn't work under Windows) Try to change permissions before:
chmod($yourfile, '0777');
unlink($yourfile);
I am working on a website using someone else's source code called ecshop, a e-commerce website. I want to use PHPUnit to unit test my code but meet a problem.
This is what the error looks like:
C:\Users\maoqiuzi\Documents\Shanglian\XinTianDi\xintiandi\admin>phpunit
--stderr wang_test.php PHPUnit 3.7.27 by Sebastian Bergmann.
E
Time: 1.03 seconds, Memory: 6.75Mb
There was 1 error:
1) ShopTest::test_get_shop_name Undefined index: ecs
C:\Users\maoqiuzi\Documents\Shanglian\XinTianDi\xintiandi\includes\lib_common.ph
p:564
C:\Users\maoqiuzi\Documents\Shanglian\XinTianDi\xintiandi\admin\includes\init.ph
p:147
C:\Users\maoqiuzi\Documents\Shanglian\XinTianDi\xintiandi\admin\wang.php:10
C:\Users\maoqiuzi\Documents\Shanglian\XinTianDi\xintiandi\admin\wang_test.php:10
FAILURES! Tests: 1, Assertions: 0, Errors: 1.
The source code of wang_test.php:
<?php
require_once("wang.php");
class ShopTest extends PHPUnit_Framework_TestCase
{
public function test_get_shop_name()
{
$shop = new Wang();
$first_row_of_shop_list = $shop->get_shop_list();
}
}
The source code of wang.php:
<?php
class Wang
{
private $exchange;
function get_shop_list()
{
define("IN_ECS", 1);
require(dirname(__FILE__).'/includes/init.php');
$this->exchange = new exchange($GLOBALS['ecs']->table('shop'), $GLOBALS['db'], 'shop_id', 'shop_name');
$sql = "SELECT * FROM " . $GLOBALS['ecs']->table('shop');
$shop_list = $GLOBALS['db']->getAll($sql);
if($shop_list != array())
return $shop_list;
else
return array();
}
}
code in init.php
require(ROOT_PATH . 'includes/lib_common.php');
class ECS //line 82
{
var $db_name = '';
var $prefix = 'ecs_';
function ECS($db_name, $prefix)
{
$this->db_name = $db_name;
$this->prefix = $prefix;
}
...
}
...
$ecs = new ECS($db_name, $prefix); // line 114
... // other initialization codes here
$_CFG = load_config(); //line 147
code in lib_common.php
function load_config()
{
$arr = array();
$data = read_static_cache('shop_config');
if ($data === false)
{
$sql = 'SELECT code, value FROM ' . $GLOBALS['ecs']->table('shop_config') . ' WHERE parent_id > 0';
$res = $GLOBALS['db']->getAll($sql);
...
}
I've been working on this for days, and felt very frustrated. Hope anyone help me out! Thanks!!!
As you can see in PHPunit's manual part related on "How to test for PHP errors", how error_reporting is configured affects the test suite; which is your case.
You have (at least) three different options:
Fix the code to check and not use undefined indexes of arrays
Change the error_reporting to ignore notices (one of the examples in the link)
Create (and use) a phpunit.xml configuration file and set convertNoticesToExceptions to false
In init.php, if you change:
$ecs = new ECS($db_name, $prefix);
to:
$GLOBALS['ecs'] = new ECS($db_name, $prefix);
does it start to work (or at least move on to a different error message)?
What I'm thinking is that init.php is expecting it is running as global code, so isn't being explicit like that, but then PHPUnit is doing something clever such that it is not run as global code. So $ecs just ends up being treated as a local variable, not as a global.
(If it does change the error message, go through all other global code in your libraries and change them to use $GLOBALS[...] explicitly too. This is a GoodThingâ„¢ to do anyway, as it makes code clearer when other people look at it, and avoids easy-to-make mistakes when refactoring global code into functions.)
I have a Yii web service actionQuery that queries a model based on four parameters. There are about 1700 items to be queried total and I'm using a pretty bad web host (iPage). When I run a query with no parameters or extremely common parameters like "a" in string name, I expect to see all or almost all of the rows. Instead I get back a 500 Internal server error that is obviously not being produced by Yii, so it's a pretty bad error. When I try to narrow it down to around 700 or 800 rows, it takes a while but it gets done. How can I correct this error of large data sets producing 500 internal server errors? Is it a max execution time issue? Is there something I need to be doing differently with CDBCriteria?
Here is actionQuery, which is admittedly coded quite poorly.
public function actionQuery()
{
$this->_checkAuth();
switch ($_GET['model'])
{
case 'dogFood':
$criteria = new CDbCriteria();
if ($_GET['name'] && $_GET['name'] !== '0') {
$criteria->addSearchCondition('name_df', $_GET['name']);
}
if ($_GET['ingredients'] && $_GET['ingredients'] !== '0') {
$ingredientsArray = explode(',',$_GET['ingredients']);
foreach ($ingredientsArray as $ingredient) {
$criteria->addSearchCondition('ingredients_df', $ingredient);
}
}
if ($_GET['brand'] && $_GET['brand'] != 0) {
$criteria->addColumnCondition(array('brand_df' => $_GET['brand']));
}
if ($_GET['brandstring'] && $_GET['brandstring'] !== 0) {
$criteriaForBrand = new CDbCriteria();
$criteriaForBrand->addSearchCondition('name_dfb', $_GET['brandstring']);
$brandInQuestion = DogfoodbrandDfb::model()->find($criteriaForBrand);
$brandId = $brandInQuestion->id_dfb;
$criteria->addColumnCondition(array('brand_df' => $brandId));
}
$models = DogfoodDf::model()->findAll($criteria);
break;
default:
$this->_sendResponse(501, sprintf(
'Error: Mode <b>query</b> is not implemented for model <b>%s</b>',
$_GET['model']));
exit;
}
if (empty($models)) {
$this->_sendResponse(200,
sprintf('No items were found for model <b>%s</b>', $_GET['model']));
}
else {
$rows = array();
foreach ($models as $model) {
$rows[] = $model->attributes;
}
$this->_sendResponse(200, CJSON::encode($rows));
}
}
Most likely you are running out of memory. There is a limit set how much memory a PHP program can use: you should see memory exceeded messages in your error.log.
You can try and up the allowed memory for a thread, or for this specific script, but verify in your error log first that this is the issue.