2013年8月10日 星期六

Install MySQL Using MacPorts for MacOS X

Install MySQL Using MacPorts

1- Install MacPorts
http://www.macports.org/install.php

2- Update MacPorts:
sudo port selfupdate

3- Install MySQL server and client binaries:
sudo port install mysql55

4- Install MySQL server startup scripts:
sudo port install mysql55-server

5- Initialize the Database:
sudo /opt/local/lib/mysql55/bin/mysql_install_db --user=mysql

6- Start MySQL:
sudo /opt/local/share/mysql55/support-files/mysql.server start

7- Set root password:
/opt/local/lib/mysql55/bin/mysqladmin -u root password 'changepasswordhere'

8- Enable the startup script so mysql starts on boot
sudo launchctl load -w /Library/LaunchDaemons/org.macports.mysql55-server.plist
– or --
sudo port load mysql55-server

9- If you need to uninstall/reinstall for whatever reason, remove the database directory when you do the uninstall
sudo port uninstall mysql55-server
cd /opt/local/var/db/
sudo rm -rf mysql55/

10- To check the contents of any package:
sudo port contents 

reference : http://jackal.livejournal.com/2160464.html

Multithreading in php

Some time ago, I’ve wrote a small server in PHP. Nothing fancy. It would listen on a socket and when a new client would connect, the server would start a new thread and manage the client’s request. Since threading it’s not available in PHP, I’ve emulated the threads with child processes which are available in php. A thread object simply encapsulates a new process started with pnctl_fork() and emulates – to some extent – the behaviour of the java.lang.Thread class, the main difference being that in my implementation, you don’t extend the Thread class, you simply provide the name of a callback function in the constructor.

A simple multithreaded application would look like this:

require_once( 'Thread.php' );

// test to see if threading is available
if( ! Thread::available() ) {
die( 'Threads not supported' );
}

// function to be ran on separate threads
function paralel( $_limit, $_name ) {
for ( $index = 0; $index < $_limit; $index++ ) {
echo 'Now running thread ' . $_name . PHP_EOL;
sleep( 1 );
}
}

// create 2 thread objects
$t1 = new Thread( 'paralel' );
$t2 = new Thread( 'paralel' );

// start them
$t1->start( 10, 't1' );
$t2->start( 10, 't2' );

// keep the program running until the threads finish
while( $t1->isAlive() && $t2->isAlive() ) {

}
This will display Now running thread 1 and Now running thread 2 messages with 1 second delays. I know, not that impressive, but hey, it’s multithreaded.

PHP threading will only work on Unix and Linux systems, because pnctl_fork is nothing more than a wrapper for the fork() function from unistd.h and it’s not available under Microsoft operating systems.

I know that the first example was pretty lame, but there are some more interesting things you could do with threads in PHP. For instance, if you need to do some server side processing of all the images in a directory, a multithreaded approach will be much faster.

require_once( 'Thread.php' );

// test to see if threading is available
if( ! Thread::available() ) {
die( 'Threads not supported' );
}

// define the function to be run as a separate thread
function processImage( $_image ) {
// expensive image processing logic here
}

$threads = array();
$index = 0;

foreach( new DirectoryIterator( '/path/to/images' ) as $item ) {
if( $item->isFile() ) {
$threads[$index] = new Thread( 'processImage' );
$threads[$index]->start( $item->getPathname() );
++$index;
}
}

// wait for all the threads to finish
while( !empty( $threads ) ) {
foreach( $threads as $index => $thread ) {
if( ! $thread->isAlive() ) {
unset( $threads[$index] );
}
}
// let the CPU do its work
sleep( 1 );
}
PS: it’s a bad practice to keep looping in order to wait for a thread to finish. An ongoing empty loop will quickly boost your CPU ’s load to 100%. If you need your processor free (and you need it), simply send the current (looping) thread to sleep and let the others execute.

// wait for thread - bad approach (overloads the CPU)
while ( $thread->isAlive() ) {
}

// wait for thread - correct approach
while( $thread->isAlive() ) {
sleep( 1 );
}
Download Thread.php
Here you go: Thread.php. Just click the link to download the class. If you have a better approach to this issue or think that the original class could be improved, don’t be shy and leave a comment below. Credits will be given.

reference : http://blog.motane.lu/2009/01/02/multithreading-in-php/

Installing mcrypt on OSX 10.8.2 - PHP 5.3.15 with MacPorts

Download and install MacPorts from http://macports.org.
The following steps are performed in the Terminal:
Force MacPorts to update (will only work if Apple's Xcode installed):
sudo port -v selfupdate
Now, install memcached:
sudo port install php5-mcrypt
Copy the newly created shared object for mcrypt into Mac OS X’s default PHP5 extension directory:
sudo cp /opt/local/lib/php/extensions/no-debug-non-zts-20090626/mcrypt.so /usr/lib/php/extensions/no-debug-non-zts-20090626/
Next, you need to edit php.ini to add the extensions. Find the phrase Dynamic Extensions, and add:
extension=mcrypt.so
And finally, restart Apache:
sudo apachectl restart
en → zh
使用MacPorts 。

wibiya widget