I've been tearing my hairs for the last 13 hours on this..
I have wrote a multi-threaded python scheduler, that listen on a socket for client commands.
On each command received, it does something, write back something to the client and close the connection. This is wrote
Code:
import Queue
import socket
import threading
import time
import sys
import os
import traceback
import random
import math
import subprocess
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM )
server.bind(( '', 2727))
server.listen(5 )
clientPool.put(server.accept() )
...
hypervisor().start()
class hypervisor (threading.Thread):
def run(self ):
client = clientPool.get() # a socket object
if client != None:
cmd=client[0].recv(1024) #read the command sent
if(cmd!=None):
print '~~~~~~'
print 'command => '+cmd
print '~~~~~~'
.... #parsing the command, and putting into the context of the to be run thread
display=self.getDisplay()
self.context['display']=display
w=webshot(id, self.context)
w.start()
ret=str(id) #The webshot thread pid
nbr=client[0].send(ret)
print '%d bytes sent => %d'%(nbr,ret)
client[0].close()
This is just an excerpt, but it works well.
I've wrote a small python client at first:
Code:
import socket
import threading
import sys
# Here's our thread:
class ConnectionThread ( threading.Thread ):
def run ( self ):
# Connect to the server:
client = socket.socket ( socket.AF_INET, socket.SOCK_STREAM )
client.connect ( ( 'localhost', skt ) )
client.send ( msg )
print client.recv ( 1024 )
client.close()
if __name__ == "__main__":
msg=sys.argv[2]
skt=int(sys.argv[1])
ConnectionThread().start()
and it works as expected. Excellent....
Now, I'm trying to integrate this with a php script, thinking that implementing socket would be a child play.
Wrong...
The base of the php script is this:
PHP Code:
class webshot{
private $url="http://www.netscape.com";
private $w=1280;
private $format='jpg';
private $qual='80';
private $crop='';
private $md5='/tmp/abc.jpg';
private $maxTime=30;
private $minTime=2;
public function __construct(){
$w=1280;
$h=1024;
$W=800;
$H=600;
$cmd=<<<CMD
url:{$this->url}, BrowsWidth:$w, BrowsHeight:$h, outW:$W, outH:$H, maxTime:{$this->maxTime}, minTime:{$this->minTime}, type:{$this->format}, qual:{$this->qual}, crop:{$this->crop}, useMC:1, file:{$this->md5}
CMD;
//$cmd=str_replace(' ','',$cmd);
$sock=fsockopen('127.0.0.1',2727);
$cpt=0;
fwrite($sock, $cmd, strlen($cmd));
print "done writing command...\n";
$pid=null;
while ($sock && !feof($sock)){
$pid.=fgets($sock,10);
}
fclose($sock);
die("webshot pid:$pid");
}
}
$w=new webshot();
Now, the sending of the order works ok.
The server receive it. I see this on it's console:
Quote:
~~~~~~
command => url:www.netscape.com, BrowsWidth:1024, BrowsHeight:768, outW:250, outH:187, maxTime:30, minTime:0, type:jpg, qual:50, crop:, useMC:1, file:63fd20cbf455a961e66796a18cbd7fe7
~~~~~~
thread pid: 52279
5 bytes sent => 52279
connection closed
|
But it's all...
The php script (ran from command line or apache) just hang after having sent the command, waiting for receiving something.
I've tried to do a print($pid) into the while() loop, where it listen on the socket, but with no success.
Even after the server closed the connection, PHP don't see it and stay waiting on something over the socket.
It bails out only when a thread monitor kills the connection thread, thinking that it's stalled.
I'd hate to resort to a shell_exec("python client.py") rather than using sockets, but for now, it's the direction I see following.
I never had problem like that with php and socket before, but it was always between 2 php processes.
Does anyone here have any idea on how to solve this issue ?