Friends
I´m build a simple application in Panda3d who´s call to server with urllib2. Well i send a request to server with threads but my frame rate goig down a lot when a thread is run … so why? I use threads for preserve my frame rate. What happend?
import direct.directbase.DirectStart
from direct.gui import *
from direct.gui.DirectGui import *
from panda3d.core import *
from direct.stdpy.threading import *
from direct.interval.IntervalGlobal import *
import socket, urllib2
from urllib import urlencode
from time import time
from sys import exit
# urllib boost
realsocket=socket.socket
def socketwrap(family=socket.AF_INET, type=socket.SOCK_STREAM, proto=0):
sockobj=realsocket(family, type, proto)
sockobj.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
return sockobj
socket.socket=socketwrap
# servers
url={"ugb":"xxx.server1", "palhaco":"xxx.server2" }
dados={"nome":"Donovan Grey"}
texto=OnscreenText(text="Waiting...", mayChange=True, pos=(-0.5, 0.8), style=2, scale=0.05, align=TextNode.ACenter)
# Call server as Threads
ioLock=Lock()
class CallServer(Thread):
def __init__(self, local):
Thread.__init__(self)
self.local=local
def run(self):
global url, dados, texto
anterior=time()
request=urllib2.Request(url[self.local], urlencode(dados))
resposta=urllib2.urlopen(request)
ioLock.acquire()
texto["text"]=resposta.read()+":"+str(time()-anterior)
ioLock.release()
palhacoThread=None
ugbThread=None
# see if thread already run. if true dont run interrupt
def callThread(nome="palhaco"):
global palhacoThread, ugbThread
thread=None
if nome=="palhaco" : thread=palhacoThread
elif nome=="ugb" : thread=ugbThread
if thread==None or thread.join():
thread=CallServer(nome)
Parallel(Func(thread.start), Wait(1.5)).start()
else:
print "Thread already in run!"
palhacoButton=DirectButton(text=("Call Palhaco", "Call Palhaco", "Call Palhaco"), scale=0.1, pos=(0.8,0,0), command=callThread, extraArgs=["palhaco"])
ugbButton=DirectButton(text=("Call UGB", "Call UGB", "Call UGB"), scale=0.1, pos=(0.8,0,-0.2), command=callThread, extraArgs=["ugb"])
base.disableMouse()
base.accept("escape", exit)
run()