def actionThread_exec(params):
t = threading.currentThread()
memory_service = getattr(t, "mem_serv", None)
# tts_service = getattr(t, "session", None).service("ALTextToSpeech")
print "Action speechbtn started with params " + params
# action init
# action init
if len(params) > 0:
memory_service.raiseEvent('AnswerOptions', 'speechbtn_' + params)
else:
memory_service.raiseEvent('AnswerOptions', 'speechbtn')
print "Action " + actionName + " " + params + " terminated"
# action end
sleep(.5)
memory_service.raiseEvent("PNP_action_result_" + actionName, "success")
python类currentThread()的实例源码
def actionThread_exec (params):
t = threading.currentThread()
memory_service = getattr(t, "mem_serv", None)
print "Action "+actionName+" "+params+" started"
# action init
if (params=='off'):
memory_service.raiseEvent(logkey,0.0)
print " -- Recording data disabled --"
else:
memory_service.raiseEvent(logkey,0.5)
print " -- Recording data enabled --"
# action init
time.sleep(1.0)
print "Action "+actionName+" "+params+" terminated"
memory_service.raiseEvent("PNP_action_result_"+actionName,"success");
def actionThread_exec (params):
t = threading.currentThread()
memory_service = getattr(t, "mem_serv", None)
print "Action "+actionName+" "+params+" started"
# action init
if (params=='off'):
memory_service.raiseEvent(asrkey,'0')
else:
memory_service.raiseEvent(asrkey,'1')
# action init
time.sleep(1.0)
print "Action "+actionName+" "+params+" terminated"
memory_service.raiseEvent("PNP_action_result_"+actionName,"success");
def actionThread_exec (params):
t = threading.currentThread()
memory_service = getattr(t, "mem_serv", None)
print "Action "+actionName+" started with params "+params
# action init
dt = 0.25
count = int(float(params) / dt)
# action init
while (getattr(t, "do_run", True) and count>0):
#print "Action "+actionName+" "+params+" exec..."
# action exec
count = count-1
# action exec
time.sleep(dt)
print "Action "+actionName+" "+params+" terminated"
# action end
count = 0
# action end
memory_service.raiseEvent("PNP_action_result_"+actionName,"success");
def actionThread_exec (params):
t = threading.currentThread()
memory_service = getattr(t, "mem_serv", None)
print "Action "+actionName+" started with params "+params
# action init
try:
vp = params.split('_')
print " -- Assign: ",vp[0]," = ",vp[1]
memory_service.insertData(vp[0],vp[1])
except:
print "ERROR in Assign parameters"
# action init
time.sleep(1.0)
print "Action "+actionName+" "+params+" terminated"
# action end
# action end
memory_service.raiseEvent("PNP_action_result_"+actionName,"success");
def rhMonitorThread (memory_service):
global last_personid
t = threading.currentThread()
print "personbehind thread started"
personid = 0
while getattr(t, "do_run", True):
v = 'false'
try:
pdist = memory_service.getData("Device/SubDeviceList/Platform/Back/Sonar/Sensor/Value")
#distance to consider that the person following
#print "rear sonar dist: ", pdist
if (pdist < 1.5):
v = 'true'
except:
v = 'false'
set_condition(memory_service,'personbehind',v)
time.sleep(0.5)
print "personbehind thread quit"
def actionThread_exec(params):
t = threading.currentThread()
memory_service = getattr(t, "mem_serv", None)
session = getattr(t, "session", None)
print "Action "+actionName+" started with params "+params
# action init
tracker_service = session.service("ALTracker")
p = params.split('_')
if p[0] == 'start':
tracker_service.setMode("Head")
tracker_service.registerTarget("Face", 0.15)
tracker_service.track("Face")
elif p[0] == 'stop':
tracker_service.stopTracker()
tracker_service.unregisterAllTargets()
print "Action "+actionName+" "+params+" terminated"
# action end
# action end
memory_service.raiseEvent("PNP_action_result_" + actionName, "success")
def rhMonitorThread (memory_service, rate, output_file):
print 'Starting recording data @%.2fHz'%rate
t = threading.currentThread()
output_file.write(str(keys_list))
output_file.write('\n')
while getattr(t, "do_run", True):
try:
values = memory_service.getListData(keys_list)
ts = time.time()
timestamp = 'timestamp: %f\n' % ts
output_file.write(timestamp)
output_file.write(str(values))
output_file.write('\n')
except:
pass
time.sleep(1.0/rate)
print "Exiting Thread Log"
def distributed_transaction_commit(*instances):
if not instances:
return
instances = enumerate(instances)
thread_key = '%s.%s' % (socket.gethostname(), threading.currentThread())
keys = ['%s.%i' % (thread_key, i) for (i,db) in instances]
for (i, db) in instances:
if not db._adapter.support_distributed_transaction():
raise SyntaxError(
'distributed transaction not suported by %s' % db._dbanme)
try:
for (i, db) in instances:
db._adapter.prepare(keys[i])
except:
for (i, db) in instances:
db._adapter.rollback_prepared(keys[i])
raise RuntimeError('failure to commit distributed transaction')
else:
for (i, db) in instances:
db._adapter.commit_prepared(keys[i])
return
def readerThread(self, d, readerNum):
if sys.version_info[0] < 3 :
name = currentThread().getName()
else :
name = currentThread().name
for i in xrange(5) :
c = d.cursor()
count = 0
rec = c.first()
while rec:
count += 1
key, data = rec
self.assertEqual(self.makeData(key), data)
rec = c.next()
if verbose:
print "%s: found %d records" % (name, count)
c.close()
if verbose:
print "%s: thread finished" % name
def readerThread(self, d, readerNum):
if sys.version_info[0] < 3 :
name = currentThread().getName()
else :
name = currentThread().name
c = d.cursor()
count = 0
rec = dbutils.DeadlockWrap(c.first, max_retries=10)
while rec:
count += 1
key, data = rec
self.assertEqual(self.makeData(key), data)
rec = dbutils.DeadlockWrap(c.next, max_retries=10)
if verbose:
print "%s: found %d records" % (name, count)
c.close()
if verbose:
print "%s: thread finished" % name
def _getresponse(self, myseq, wait):
self.debug("_getresponse:myseq:", myseq)
if threading.currentThread() is self.sockthread:
# this thread does all reading of requests or responses
while 1:
response = self.pollresponse(myseq, wait)
if response is not None:
return response
else:
# wait for notification from socket handling thread
cvar = self.cvars[myseq]
cvar.acquire()
while myseq not in self.responses:
cvar.wait()
response = self.responses[myseq]
self.debug("_getresponse:%s: thread woke up: response: %s" %
(myseq, response))
del self.responses[myseq]
del self.cvars[myseq]
cvar.release()
return response
def readerThread(self, d, readerNum):
if sys.version_info[0] < 3 :
name = currentThread().getName()
else :
name = currentThread().name
for i in xrange(5) :
c = d.cursor()
count = 0
rec = c.first()
while rec:
count += 1
key, data = rec
self.assertEqual(self.makeData(key), data)
rec = c.next()
if verbose:
print "%s: found %d records" % (name, count)
c.close()
if verbose:
print "%s: thread finished" % name
def readerThread(self, d, readerNum):
if sys.version_info[0] < 3 :
name = currentThread().getName()
else :
name = currentThread().name
c = d.cursor()
count = 0
rec = dbutils.DeadlockWrap(c.first, max_retries=10)
while rec:
count += 1
key, data = rec
self.assertEqual(self.makeData(key), data)
rec = dbutils.DeadlockWrap(c.next, max_retries=10)
if verbose:
print "%s: found %d records" % (name, count)
c.close()
if verbose:
print "%s: thread finished" % name
def _getresponse(self, myseq, wait):
self.debug("_getresponse:myseq:", myseq)
if threading.currentThread() is self.sockthread:
# this thread does all reading of requests or responses
while 1:
response = self.pollresponse(myseq, wait)
if response is not None:
return response
else:
# wait for notification from socket handling thread
cvar = self.cvars[myseq]
cvar.acquire()
while myseq not in self.responses:
cvar.wait()
response = self.responses[myseq]
self.debug("_getresponse:%s: thread woke up: response: %s" %
(myseq, response))
del self.responses[myseq]
del self.cvars[myseq]
cvar.release()
return response
def gitshell_watcher(self):
t = threading.currentThread()
logging.debug("Watcher thread init {}".format(t))
while getattr(t, "do_run", True):
if not self.data_q.empty():
logging.debug("Polling Queue for Closed Issues")
comment_list=ghlib.getClosedIssueComments(
self.git_repo,
self.data_q.get())
if comment_list:
for comment in comment_list:
print(comment)
logging.debug("Polling Wait for {} ".
format(self.rtm_poll_freq))
sleep(self.rtm_poll_freq)
logging.debug("Watcher thread de-init {}".format(t))
return
def _cobra_http_getsock(self):
thr = currentThread()
tsocks = getattr(thr, 'cobrahttpsocks', None)
if tsocks == None:
tsocks = {}
thr.cobrahttpsocks = tsocks
sock = tsocks.get(self._cobra_slookup)
if not sock or sock.trashed:
# Lets build a new socket... shall we?
sock = self._cobra_http_newsock()
# If we have authinfo lets authenticate
authinfo = self._cobra_kwargs.get('authinfo')
if authinfo != None:
sock.authUser(authinfo)
tsocks[self._cobra_slookup] = sock
return sock
def boredthread(func):
"""
The same as "workthread" above, but drop the request on the
floor if the worker thread already has better things to do...
"""
# If we're already the work thread, just do it.
def workadd(*args, **kwargs):
if getattr(currentThread(), 'VQtWorkerThread', False):
return func(*args, **kwargs)
if not len(workerq):
workerq.append((func, args, kwargs))
functools.update_wrapper(workadd, func)
return workadd
def workerThread():
# We are *not* allowed to make Qt API calls
currentThread().VQtWorkerThread = True
while True:
try:
todo = workerq.get()
if todo is not None:
func, args, kwargs = todo
if func is None:
return
func(*args, **kwargs)
except Exception as e:
traceback.print_exc()
print(('vqt worker warning: %s' % e))
def startup(css=None):
# yea yea.... globals suck...
global qapp # the main QApplication
global guiq # queue of GUI calls to proxy
global ethread # QtThread that consumes guiq
global workerq # queue of "worker" calls to proxy
guiq = e_threads.EnviQueue()
workerq = e_threads.EnviQueue()
currentThread().QtSafeThread = True
qapp = VQApplication(sys.argv)
if css:
qapp.setStyleSheet(css)
ethread = QEventThread(guiq)
ethread.idleadd.connect(qapp.callFromQtLoop)
ethread.start()
workerThread()