Related
I'm writing an C++ extension (dynamic load) for HHVM. I followed the instructions on this page:
https://github.com/facebook/hhvm/wiki/Extension-API
which links to an example in:
https://github.com/hhvm/extension-example
I compiled hhvm on Ubuntu 14.04 which took nearly 2 hours. Then I also compiled the example extension.
My question is, how to load it?
The information on the internet seems to be out-of-date or inconsistent.
Anyway, I first tried to create /etc/hhvm/config.hdf with these lines:
DynamicExtensions {
example = /path/to/example.so
}
Nothing happened. And then I saw this:
From: http:// hhvm.com/blog/4349/hhvm-3-0-0
We are moving from .hdf config files to .ini. The default one lives in
/etc/hhvm/php.ini. We don’t support all the old options yet, so you
can still use config.hdf for now, but be ready for it to die in the
next release. All of your favorite options will go from Foo { BarBaz =
True } to hhvm.foo.bar_baz = true.
OK, then I tried to put lines in /etc/hhvm/php.ini or /etc/hhvm/server.ini instead of .hdf
hhvm.dynamic_extensions.example = /path/to/example.so
But with no luck, nothing worked. I need more info/docs.
So, is there anyone know what happen? or if the HHVM team from Facebook see this post, could you please help me?
I see that you've managed to get it to load, so I'll just focus on not being able to find the function.
Shortly after the release of HHVM 3.0, the way that PHP files are loaded from extensions changed. Basically, the first four characters of the name of the file are stripped when embedding it, since it's expected to be ext_name.php. The example extension hadn't been updated for this change until last night.
The change is rather simple. Just rename example.php to ext_example.php and, in config.cmake change HHVM_SYSTEMLIB(example example.php) to HHVM_SYSTEMLIB(example ext_example.php) then re-run cmake . && make.
You can see the committed change (which does exactly this) here
I am using wampserver to test & run wordpress code in my local computer. In order to run pthread, I have followed the following steps:
1) I got the pthread zip file from http://windows.php.net/downloads/pecl/releases/pthreads/0.44/
(My machine has php 5.3.13 and downloaded the php_pthreads-0.44-5.3-ts-vc9-x86.zip file from the above link).
2) Extracted the zip file. Moved the php_pthreads.dll to the C:\wamp\bin\php\php5.3.13\ext directory.
3) Moved pthreadVC2.dll to the C:\wamp\bin\php\php5.3.13 directory.
4) Then Opened C:\wamp\bin\php\php5.3.13\php.ini and added the code extension=php_pthreads.dll at the begining of the file.
But when I try to run the following code:
<?php
class My extends Thread {
public function run() {
printf("%s is Thread #%lu\n", __CLASS__, $this->getThreadId());
}
}
$my = new My();
$my->start();
?>
It gives me the following error:
Fatal error: Class 'Thread' not found in C:\wamp\www\wp-admin\includes\post.php on line 2
Can you please tell me how to install pthreads in my computer to use with php? and do I have to install any other software?
I've noticed that wampserver has php.ini in two separate places. One place is in the /wamp/bin/php/php5... directory, and the other place is in the /wamp/bin/apache/apache.../bin directory (where "..." represents version numbers). The two files need to be identical, because apparently both are loaded at different times by the overall wampserver boot-up procedure.
(Note I only discovered this recently, and may be well "behind the curve" of doing fancy things with wampserver --maybe everyone else has been dealing with both files for a long time. So I don't know if this simple thing will fix your problem; I came here looking for info, myself, regarding doing some multi-threading stuff. :)
One other thing. According to this page: www.php.net/manual/en/pthreads.requirements.php
PHP has to be compiled with "--enable-zts" in order for pthreads stuff to work. I have not been able to find any evidence that the PHP part of wampserver was compiled that way.
(months later)
Having decided I didn't really immediately need to do any threading stuff, I went on to do other things, until the need actually arose. I now can say that the version of PHP compiled into WampServer does support the "pthread" extension, although some set-up work is needed, first. The instructions I saw mentioned putting a couple of .dll files (after a download and unZip) into certain places, but that didn't work for me. Copying them to the \Windows\System32 directory did work. (Putting them into the \apache...\bin directory also works; there are some other php .dll files in there.)
After that, much like what you did, it is necessary to define a "class" that extends the "Thread" class, in order to actually do something in another thread. The "run()" function in the Thread class is "abstract", and needs to be "realized" as an actual function in the extended class. Then the "new" operator can create an "instance", an object of that specified class, for actual use. Here's the class that I needed:
//Purpose: Use another thread to run the code in another php file, after a delay
class xT extends Thread
{ var $fil, $tim;
function savWhatWhen($f="", $t=0)
{ $this->fil = $f; //save What, file to process
$this->tim = $t; //save When, delay before processing file
return;
}
function run()
{ ini_set('max_execution_time', 600); //600 seconds = 10 minutes
sleep($this->tim); //do a delay; beware of max-exec-time!
include($this->fil); //load file-to-process, and process it
return;
} }
That "savWhatWhen()" function was created specifically for this extension of the basic Thread class. Here's some code for using that class:
$TH = new xT(); //prepare a separate Thread
$TH->savWhatWhen("d:/wamp/myscripts/test.php", 45);//file-name and delay time
$TH->start(); //after delay, process file
//the code that does this can terminate, while OTHER thread is doing a delay
Note for anyone copying this code, you might need to make sure your "open_basedir" setting in the php.ini allows access to the specified file.
More months later: With lots of things being worked on, I haven't put a lot of time into using my pthread object. I did encounter a peculiarity that makes me wonder about whether or not I can actually use pthreads the way I had hoped. Here is what I have observed:
1. An initial php file is called by AJAX, to do something.
2. The PHP processor on the Web Server does that thing.
3. Various data is supposed to be echoed to the browser.
4. The initial php file calls for the creation of another thread, and terminates.
5. The browser does not yet receive the echoed data!
6. The PHP processor on the Web Server does the work delegated to the second thread.
7. When the second thread terminates, NOW the browser receives the echoed data!
At this writing I'm thinking I missed something. Perhaps I need to do some forceful "flush" stuff when the first thread ends, so that the browser can receive the echoed data and the user can do things while the PHP processor on the server is also doing things.
Check for extension_dir = "ext" in you php.ini file. Make sure it points to the folder where your extensions reside and make sure it's not commented (it has a semicolon ; in front of it)
You have to add a require_once() with the path of the Thread class before extending it (if your framework don't use an autoload class system)
I've encountered the same problem, in my case placing the pthreadVC2.dll in
..wamp\bin\apache\Apache2.4.4\bin
(instead of ..\wamp\bin\php\php5.4.16 as the guide in php.net instructs) solved the problem
Wamp server has a separate php.ini config file for the browser and for the cli.
To use the pthreads module in the browser with WAMP Server you need to copy the "pthreadVC2.dll" into the apache "bin" directory also.
You should now have he "pthreadVC2.dll" in both of these folders (if installed in default location):
C:\wamp\bin\php\php[x.x.xx]\bin
C:\wamp\bin\apache\apache[x.x.x]\bin
You will also need to update the php.ini file within the php bin directory AND the apache bin directory to include:
extension=php_pthreads.dll
This now means you can use pthreads in the browser and in the cli with wamp server
After encountering the same problem, I noticed that I have installed the wrong Pthread version (3.1.6 : requires PHP7+) which wasn't compatible with my PHP version (5.5.12). Solved the problem with Pthread version 0.0.44. An earlier version should probably work well.
Here is the download page for Pthread and the installation page. Be careful about the both php.ini location as mentioned above (Apache folder=for Browser, PHP folder=CLI).
I do have a PHP script, which is not an extension for Typo3. Now I would like to delete the whole Cache of Typo3 out of this script. How is that possible?
install the TYPO3 Extension cleartypo3cache
create a tool and a keyboard shortcut in PhpStorm 4 to trigger cleartypo3cache
SSH access with passwordless pubkey authentication when pushing to a remote host.
Install Extension "cleartypo3cache" and create the BE user "_cli_cleartypo3cache" and add the following TSconfig:
options.clearCache.all=1
options.clearCache.pages=1
Now test if cache is cleared:
$ cd /path/tp/typo3-site/
$ php typo3/cli_dispatch.phpsh cleartypo3cache all
If your webserver is on localhost, you are lucky because you don't need this shell script. If your webserver is on a remote host, you need an additional wrapper script. This is because PhpStorm does not provide an environment variable for the remote host directory. You have to set this directory statically for each project in the wrapper script:
#!/bin/sh
TYPO3_SITE_PATH="/path/to/typo3-site"
USER="alice"
HOST="example.com"
/usr/bin/ssh $USER#$HOST '/usr/bin/php $TYPO3_SITE_PATH/typo3/cli_dispatch.phpsh cleartypo3cache all'
Save this file in your project file directory into .idea/clear-typo3-cache.sh and make it executable:
$ chmod 755 .idea/clear-typo3-cache.sh
PhpStorm External Tools
You need to create an "external tool" in PhpStorm to be able to clear cache.
Go to PhpStorm-->Settings-->External Tools-->Add...
Give your tool a name and a group, e.g. "Deployment" -> "Clear TYPO3 Cache"
Deactivate checkbox "Open Console" and "Menu->Search Results"
Remote host scenario
Add the following line to "Programm:"
$ProjectFileDir$/.idea/clear-typo3-cache.sh
Localhost scenario
Add this line to "Program:"
$PhpExecutable$
Add this line to "Parameters:"
$ProjectFileDir$/typo3/cli_dispatch.phpsh cleartypo3cache all
You need to have a PHP interpreter configured in PhpStorm-->Settings-->PHP to use $PhpExecutable$. Alternatively you can use /usr/bin/php
(source: t3node.com)
PhpStorm Keymap
I suggest to use the same key binding as you use for saving or remote host uploading:
Go to PhpStorm-->Settings-->Keymap
For remote host scenario, navigate to: Main menu-->Tools-->Deployment-->Upload to Default Server. Notice the existing shortcut. If you don't have one for that, create a new one (I use ALT+SHIFT+U)
For the localhost scenario, just use Ctrl+S (Main menu-->File-->Save All).
Now navigate to the External Tool you have created (e.g. External Tools-->Deployment->Clear TYPO3 Cache)
Right click "Add Keyboard Shortcut"
Create the particular shortcut in "First Stroke"
Now PhpStorm will warn you that the shortcut is already in use for a different command. That's fine, it's exactly what we want to have.
That's it. Your TYPO3 caches are always cleared when you hit save or upload on your keyboard.
adapted from t3node
I found the solution myself and its actually pretty easy. I took a look into the class.t3lib_tcemain.php in the t3lib folder. There you've got the necessary commands to clear the cache. It also checks, if you have the cachingframework enabled. If so, you need to truncate a few other tables as well (Starts with cachingframework_cache_)
It is basically:
<?php
require_once('./typo3conf/localconf.php');
$conn = mysql_connect($typo_db_host, $typo_db_username, $typo_db_password);
mysql_select_db($typo_db);
// Clear Cache here
mysql_query("TRUNCATE cache_treelist;");
mysql_query("TRUNCATE cache_pagesection;");
mysql_query("TRUNCATE cache_hash;");
mysql_query("TRUNCATE cache_pages;");
if($handle = opendir('./typo3conf')) {
while (false !== ($file = readdir($handle))) {
if(strpos($file, 'temp_CACHED_')!==false) {
unlink('./typo3conf/'.$file);
}
}
closedir($handle);
}
?>
TYPO3 >= 7
From TYPO3 7 on you can install Helmut Hummels Extension typo3_console.
Then you can clear the cache like (for composer installations):
./vendor/bin/typo3cms cache:flush
https://extensions.typo3.org/extension/typo3_console/
https://github.com/TYPO3-Console/TYPO3-Console
TYPO3 6.x
first initialize the Service in your Class
/**
* #var Tx_Extbase_Service_CacheService
*/
protected $cacheService;
/**
* #param Tx_Extbase_Service_CacheService $cacheService
* #return void
*/
public function injectCacheService(Tx_Extbase_Service_CacheService $cacheService) {
$this->cacheService = $cacheService;
}
in your function just call
$this->cacheService->clearPageCache($pids);
while $pids is an integer (for single page) or array of integers (multiple pages)
see: http://typo3.org/api/typo3cms/class_t_y_p_o3_1_1_c_m_s_1_1_extbase_1_1_service_1_1_cache_service.html
In TYPO3 since 4.5 (I think) its a static method so you have just to call
Tx_Extbase_Utility_Cache::clearPageCache($pids);
in your controller.
Found it here: http://www.phpkode.com/source/p/typo-cms/typo3_src+dummy-4.6.5/typo3/sysext/extbase/Classes/MVC/Controller/ActionController.php
In FLOW3 there is a possibility to do such stuff, as far as I know with TYPO3 v.4.x You have no such default CLI option, so You should use or You own script, or use such extensions as cleartypo3cache or Cli Cleaner.
Also I made a bash script to clean cache tables of Your dB : https://gist.github.com/fedir/5162747
in typo3 6.x extbase its simple.
Edit : clearPageCache is not static then you need to create object of CacheService
TYPO3\CMS\Extbase\Service\CacheService::clearPageCache(pageUid);
I've recently started using APC cache on our servers. One of the most important parts of our product is a CLI (Cron/scheduled) process, whose performance is critical. Typically the batchjob consists of running some 16-32 processes in parallel for about an hour (they "restart" every few minutes).
By default, using APC cache in CLI is a waste of time due to the opcode cache not being retained between individual calls. But APC also contains apc_bin_dumpfile() and apc_load_dumpfile() functions.
I was thinking these two function might be used to make APC efficient in CLI mode by having it all compiled sometime outside the batchjob, stored in a single dumpfile and having the individual processes load the dumpfile.
Does anybody have any experience with such a scenario or can you give good reasons why it will or will not work? If any significant gains could reasonably be had, either in memory use or performance? What pitfalls are lurking in the shadows?
Disclaimer: As awesome as APC is when it works in CLI, and it is awesome, it can equally be as frustrating. Use with a healthy load of patience, be thorough, step away from the problem if you're spinning, keep in mind you are working with cache that is why it seems like its doing nothing, it is actually doing nothing. Delete dump file, start with just the basics, if that doesn't work forget it try a new machine, new OS, if it is working make a copy, piece by piece expand functionality - there are loads of things that won't work, if it is working commit or make a copy, add another piece and test again, for sanity-check recheck the copies that were working before, cliches or not; if at first you don't succeed try try again, you can't keep doing the same thing expecting new results.
Ready? This is what you've been waiting for:
Enable apc for cli
apc.enable-cli=1
it is not ideal to create, populate and destroy the APC cache on every CLI request
- previous answer by unknown poster since removed.
You're absolutely right that sucks, lets fix it shall we?
If you try and use APC under CLI and it is not enabled you will get warnings.
something like:
PHP Warning: apc_bin_loadfile(): APC is not enabled,
apc_bin_loadfile not available.
PHP Warning: apc_bin_dumpfile(): APC is not enabled,
apc_bin_dumpfile not available.
Warning: I suggest you don't enable cli in php.ini, it is not worth the frustration, you are going to forget you did it and have numerous other headaches with other scripts, trust me its not worth it, use a launcher script instead. (see below)
apc_loadfile and apc_dumpfile in cli
As per the comment by mightye php we need to disable apc.stat or you will get a warnings
something like:
PHP Warning: apc_bin_dumpfile(): Excluding some files from apc_bin_dump[file].
Cached files must be included using full path with apc.stat=0.
launcher script - php-apc.sh
We will use this script to launch our apc enabled scripts (ex. ./php-apc.sh apc-cli.php) instead of changing the properties in php.ini directly.
#/bin/sh
php -d apc.enable_cli=1 -d apc.stat=0 $1
Ready for the basic functionality? Sure you are =)
basic APC persisted - apc-cli.php
<?php
/** check if dump file exists, you don't want to use file_exists */
if (false !== $dump_file = stream_resolve_include_path('apc.dump'))
/** so where were we lets have a look see shall we */
if (false !== apc_bin_loadfile($dump_file))
/** fetch what was stored last run just for fun */
if (false !== $value = apc_fetch('my.awesome.apc.store'))
echo "$value from apc\n";
/** store what gets fetched the next run just for fun */
apc_store('my.awesome.apc.store', 'awesome in cli');
/** what a shlep lets not do that all over again shall we */
apc_bin_dumpfile(array(),null,'apc.dump');
Notice: Why not use file_exists? Because file_exists == stat you see and we want to reap the reward that is apc.stat=0 so; work within the include path; use absolute and not relative paths - as returned by stream_resolve_include_path(); avoid include_once, require_once use the non *_once counterparts; check your stat usage, when not using APC(Muchos important senor), with the help of a StreamWrapper echo for calls to method url_stat; Oops: Fatal scope over-run error! aborting notice thread. see url_stat
message: Error caused by StreamWrapper outside the scope of this discussion.
The smoke test
Using the launcher execute the basic script
./php-apc.sh apc-cli.php
A whole bunch of nothing happened that's what we want right, why else do you want to use cache? If it did output anything then it didn't work, sorry.
There should be a dump file called apc.dump see if you can find it? If you can't find it then it didn't work, sorry.
Good we have the dump file there were no errors lets run it again.
./php-apc.sh apc-cli.php
What you want to see:
awesome in cli from apc
Success! =)
There are few in PHP as satisfying as a working APC implementation.
nJoy!
I would definitely not use it in the CLI as when you restart it, it's almost as if it was never running in the first place!
The better way of using APC is to have it running on the webserver itself all the time, this way with it being active it will actually do what it's supposed to do!
I tryed with curl and APC.it works
use these commands in CLI
curl --data "param1=value2" http://testsite.com/test.php
so it will post data to test.php and you writes the code in it.
since a few hours our server hangs every time you do a session_start.
For testing purposes i created a script which looks like this:
<?php
session_start();
?>
Calling it from the console hangs and it can't even be stopped with ctrl-c, only kill -9 works. The same for calling it via Apache. /var/lib/php/session/ stays empty but permissions are absolutely fine, www can write and also has read permissions for all parent folders.
According to the admins there were no changes made on the server and there is no special code registered for sessions. The Server is CentOS 4 or 5 and yesterday everything was working perfectly. We rebooted the server and updated PHP, but nothing changed.
I've ran out of ideas, any suggestions?
UPDATE
We solved this problem by moving the project to another server, so while the problem still exists on one server there is no immediate need for a solution anymore.
I will keep the question open in case someone has an idea for others having a similar problem in the future, though.
There are many reasons for that, here are a few of them:
A. The session file could be opened exclusively.
When the file lock is not released properly for whatever reason, it is causing session_start() to hang infinitely on any future script executions.
Workaround: use session_set_save_handler() and make sure the write function uses fopen($file, 'w') instead of fopen($file, 'x')
B. Never use the following in your php.ini file (entropie file to "/dev/random"), this will cause your session_start() to hang:
<?php
ini_set("session.entropy_file", "/dev/random");
ini_set("session.entropy_length", "512");
?>
C.
session_start() needs a directory to write to.
You can get Apache plus PHP running in a normal user account. Apache will then of course have to listen to an other port than 80 (for instance, 8080).
Be sure to do the following things:
- create a temporary directory PREFIX/tmp
- put php.ini in PREFIX/lib
- edit php.ini and set session.save_path to the directory you just created
Otherwise, your scripts will seem to 'hang' on session_start().
If this helps:
In my scenario, session_start() was hanging at the same time I was using the XDebug debugger within PHPStorm, the IDE, on Windows. I found that there was a clear cause: Whenever I killed the debug session from within PHPStorm, the next time I tried to run a debug session, session_start() would hang.
The solution, if this is your scenario, is to make sure to restart Apache every time you kill an XDebug session within your IDE.
I had a weird issue with this myself.
I am using CentOS 5.5x64, PHP 5.2.10-1. A clean ANSI file in the root with nothing other than session_start() was hanging. The session was being written to disk and no errors were being thrown. It just hung.
I tried everything suggested by Thariama, and checked PHP compile settings etc.
My Fix:
yum reinstall php; /etc/init.d/httpd restart
Hope this helps someone.
To everyone complaining about the 30 seconds of downtime being unacceptable, this was an inexplicable issue on a brand new, clean OS install, NOT a running production machine. This solution should NOT be used in a production environment.
Ok I face the same problem on 2 PC, 1 is MAC mini XAMPP, 1 is Windows 10 Xampp.
Both is php spent infinity to run session_start(). Both PHP version is 7.x.x
I found that session files is lock to read and write. So that I added code to make PHP read session files and immediately unlock when done with
<?php
session_start([
'read_and_close' => true,
]);
?>
or
<?php
//For PHP 5.x
session_start();
session_write_close();
?>
After this PHP unlock session file => Problems solve
The problem: -
Iv experienced (and fixed) the problem where file based sessions hang the request, and database based sessions get out of sync by storing out of date session data (like storing each session save in the wrong order).
This is caused by any subsequent request that loads a session (simultaneous requests), like ajax, video embed where the video file is delivered via php script, dynamic resource file (like script or css) delivered via php script, etc.
In file based sessions file locking prevents session writing thus causing a deadlock between the simultaneous request threads.
In database based session the last request thread to complete becomes the most recent save, so for example a video delivery script will complete long after the page request and overwrite the since updated session with old session data.
The fix: -
If your ajax or resource delivery script doesnt need to use sessions then easiest to just remove session usage from it.
Otherwise you'd best make yourself a coffee and do the following: -
Write or employ a session handler (if not already doing so) as per http://www.php.net//manual/en/class.sessionhandler.php (many other examples available via google search).
In your session handler function write() prepend the code ...
// processes may declare their session as read only ...
if(!empty($_SESSION['no_session_write'])) {
unset($_SESSION['no_session_write']);
return true;
}
In your ajax or resource delivery php script add the code (after the session is started) ...
$_SESSION['no_session_write'] = true;
I realise this seems like a lot of stuffing around for what should be a tiny fix, but unfortunately if you need to have simultaneous requests each loading a session then it is required.
NOTE if your ajax or resource delivery script does actually need to write/save data, then you need to do it somewhere other than in the session, like database.
Just put session_write_close(); befor Session_start();
as below:
<?php
session_write_close();
session_start();
.....
?>
I don't know why, but changing this value in /etc/php/7.4/apache2/php.ini worked for me:
;session.save_path = "/var/lib/php/sessions"
session.save_path = "/tmp"
To throw another answer into the mix for those going bananas, I had a session_start() dying only in particular cases and scripts. The reason my session was dying was ultimately because I was storing a lot of data in them after a particularly intensive script, and ultimately the call to session_start() was exhausting the 'memory_limit' setting in php.ini.
After increasing 'memory_limit', those session_start() calls no longer killed my script.
For me, the problem seemed to originate from SeLinux. The needed command was chcon -R -t httpd_sys_content_t [www directory] to give access to the right directory.
See https://askubuntu.com/questions/451922/apache-access-denied-because-search-permissions-are-missing
If you use pgAdmin 4 this can happen as well.
If you have File > Preferences > SQL Editor > Options > "Auto Commit" disabled, and you just ran a query using the query tool but didn't manually commit, then session_start() will freeze.
Enable auto commit, or manually commit, or just close pgAdmin, and it will no longer freeze.
In my case it seems like it was the NFS Share that was locking the session , after restarting the NFS server and only enabled 1 node of web clients the sessions worked normally .
Yet another few cents that might help someone. In my case I was storing in $_SESSION complex data with several different class objects in them and session_start() couldn't handle the whole unserialization as not every class was loaded on session_start. The solution is my case was to serialize/jsonify data before saving it into the $_SESSION and reversing the process after I got the data out of session.