Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
A python server, a PHP client and socket between them: no results in PHP
Old 08-16-2008, 10:14 AM A python server, a PHP client and socket between them: no results in PHP
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
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$cmdstrlen($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 ?
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
 
Register now for full access!
Old 08-16-2008, 04:22 PM Re: A python server, a PHP client and socket between them: no results in PHP
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Ha !
I've found it....

I had to add a "\n" after each infos sent back. Php stop to read when he received a EOL character...
So much time lost over such a litlle detail...
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 08-16-2008, 05:58 PM Re: A python server, a PHP client and socket between them: no results in PHP
wayfarer07's Avatar
Poo on You

Latest Blog Post:
Introducing WowWindow
Posts: 3,987
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
Har har, you were the only one here who could answer your own question.... figures
__________________
I build web things. I work for the startup
Please login or register to view this content. Registration is FREE
.
wayfarer07 is online now
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Old 08-16-2008, 07:33 PM Re: A python server, a PHP client and socket between them: no results in PHP
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
I'm too tired to figure anything left.
I have just coded my first multi-threaded daemon (and it's my first really big app in python !!) and adapted an existing application in 3 nights, and sleeping only 3 to 4 hours per night.

This was a bit critical, but I just finished putting it on the pre-prod server, and everything seems to be running like a charm.
I love it !
And I'm pretty proud of myself that I somehow managed to avoid the most evident caveat of multi-threading. At least., I did not have ran into any until now.
I never did that before, and I was a bit angry that it was on the php side (the one I mostly master) that the problem arose...
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 08-17-2008, 09:43 AM Re: A python server, a PHP client and socket between them: no results in PHP
wayfarer07's Avatar
Poo on You

Latest Blog Post:
Introducing WowWindow
Posts: 3,987
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
Quote:
I had to add a "\n" after each infos sent back.
It's always the little things that are the most confusing, it seems.
__________________
I build web things. I work for the startup
Please login or register to view this content. Registration is FREE
.
wayfarer07 is online now
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Reply     « Reply to A python server, a PHP client and socket between them: no results in PHP
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.15448 seconds with 12 queries