Thread: PHP to Python?
View Single Post
Old Sep 23rd, 2007, 1:41 AM   #5
Game_Ender
Professional Programmer
 
Game_Ender's Avatar
 
Join Date: May 2006
Location: Maryland, USA
Posts: 306
Rep Power: 3 Game_Ender is on a distinguished road
When I said indentation I was referring to the leaving of statements one line, no need to do so in python. It just increases the line noise to code ratio, which seemed high to my non-PHP trained eyes.

Quote:
Your code that you have posted is pretty much a match to mine but thanks anyway..
It is a corrected version of the code you posted. I did not run it but you appeared to be using "recvfrom" improperly and you are still overwriting the data you get from the server (data = s.recv(1024) throws out the previous received data). It will also give you an error when the socket returns None, as it does when it gets no data.

Here is a version of the code the implements the proper waiting semantics and is well commented.

python Syntax (Toggle Plain Text)
  1.  
  2. def Update(s):
  3. # Request Status
  4. s.send("\\status\\")
  5.  
  6. # Buffer to hold incoming data
  7. data = ''
  8.  
  9. start_time = time.time()
  10. while True:
  11. # Attempt to get new data
  12. new_data = s.recv(1024)
  13.  
  14. # Handle no new data case
  15. if not new_data:
  16. # Drop out its been two seconds
  17. if (time.time() - start_time) > 2:
  18. break
  19. else:
  20. # Sleep 100 micro seconds
  21. time.sleep(0.0001)
  22.  
  23. # Append new data to buffer
  24. data += new_data
  25.  
  26. # Check for termination string
  27. if data.find("final\\")
  28. break
  29.  
  30. s.close()
  31.  
  32. return data.split('\\')

I don't think the odd waiting code is needed, the socket timeout is should be enough. If you set the timeout to be 2 seconds the socket will block for 2 seconds to wait for new data.
__________________
Robotics @ Maryland AUV Team - Software Lead
Game_Ender is offline   Reply With Quote