How to use _worker method in ATX

Best Python code snippet using ATX

CreateMultipleOrderedEC.py

Source:CreateMultipleOrderedEC.py Github

copy

Full Screen

1#!/usr/bin/env python2# -*- coding: utf-8 -*-3##4# @file CreateMultipleOrderedEC.py5# @brief 実行順序の設定ができる実行コンテキスト6import threading7import time8import OpenRTM_aist9from MPTask import MPTask,GUITask10import MPComp11pyqtExist = True12try:13 from PyQt4 import QtCore, QtGui14except:15 pyqtExist = False16##17# @class MultipleOrderedEC18# @brief 実行順序の設定ができる実行コンテキスト19#20class MultipleOrderedEC(OpenRTM_aist.PeriodicExecutionContext):21 ##22 # @brief コンストラクタ23 # @param self 24 def __init__(self):25 26 OpenRTM_aist.PeriodicExecutionContext.__init__(self)27 28 29 30 self._rtcout = OpenRTM_aist.Manager.instance().getLogbuf("rtobject.mp_ec")31 self.prop = OpenRTM_aist.Manager.instance().getConfig()32 #print OpenRTM_aist.Manager.instance().getConfig()33 self.SetGui = "YES"34 self.FileName = ""35 #self.DebugFlag = ""36 self.SetGui = self.getProperty(self.prop, "exec_cxt.periodic.gui", self.SetGui)37 #print self.SetGui38 self.FileName = self.getProperty(self.prop, "exec_cxt.periodic.filename", self.FileName)39 #print self.FileName40 #self.DebugFlag = self.getProperty(self.prop, "exec_cxt.periodic.debug", self.DebugFlag)41 42 self.rs = []43 self.r_num = 044 45 46 self.SetGui = [self.SetGui]47 OpenRTM_aist.eraseBlank(self.SetGui)48 self.SetGui = self.SetGui[0]49 self.FileName = [self.FileName]50 OpenRTM_aist.eraseBlank(self.FileName)51 self.FileName = self.FileName[0]52 53 #self.DebugFlag = [self.DebugFlag]54 #OpenRTM_aist.eraseBlank(self.DebugFlag)55 #self.DebugFlag = self.DebugFlag[0]56 self._mutex_del2 = threading.RLock()57 if self.SetGui == "YES":58 #print pyqtExist59 if pyqtExist:60 self.g_task = GUITask(self)61 self.g_task.activate()62 self.nameList = []63 self.comp_t = []64 65 ##66 # @brief rtc.confの設定を取得する関数67 # @param self 68 # @param prop プロパティ69 # @param key キー70 # @param value 値71 # @return 値72 def getProperty(self, prop, key, value):73 74 if prop.findNode(key) != None:75 #print value76 value = prop.getProperty(key)77 return value78 79 ##80 # @brief RTCのリスト更新81 # @param self 82 def Update_Name(self):83 self._worker.updateComponentList()84 85 if self.comp_t == self._worker._comps:86 pass87 else:88 guard2 = OpenRTM_aist.ScopedLock(self._mutex_del2)89 self.comp_t = []90 self.nameList = []91 for i in range(0, len(self._worker._comps)):92 self.comp_t.append(self._worker._comps[i])93 self._worker._comps[i].i_name = self._worker._comps[i]._rtobj.get_component_profile().instance_name94 #self.nameList.append(self._worker._comps[i]._sm._obj.get_component_profile().instance_name)95 del guard296 97 ##98 # @brief 番号からコンポーネントの名前取得の関数99 # @param self100 # @param num 番号101 # @return RTC名102 def getCompName(self, num):103 104 self.Update_Name()105 106 107 #guard2 = OpenRTM_aist.ScopedLock(self._mutex_del2)108 #Name = self._worker._comps[num]._sm._obj.get_component_profile().instance_name109 #del guard2110 #Name = self.nameList[num]111 Name = self._worker._comps[num].i_name112 return Name113 114 ##115 # @brief コンポーネントの数取得の関数116 # @param self117 # @return RTC数118 def getCompNum(self):119 return len(self._worker._comps)120 121 ##122 # @brief コンポーネントのロジック実行の関数123 # @param self124 # @param c ブロック125 def workerComp(self, c):126 sd = c.r in self._worker._comps127 128 #if self.DebugFlag == "YES":129 # print c.v130 #print c.v, c.r131 if sd == True:132 #t0_ = OpenRTM_aist.Time()133 c.r._sm.worker()134 #t1_ = OpenRTM_aist.Time()135 #print c.v,(t1_ - t0_).getTime().toDouble()136 else:137 138 for i in range(0, len(self._worker._comps)):139 if c.v == self.getCompName(i):140 c.r = self._worker._comps[i]141 self._worker._comps[i]._sm.worker()142 143 ##144 # @brief 設定した実行順序のRTCを格納する関数145 # @param self146 def LoadRules(self):147 for h in range(0, len(self.rs)):148 for i in range(0, len(self.rs[h].ar)):149 for j in range(0, len(self._worker._comps)):150 151 #Name = self._worker._comps[j]._sm._obj.get_component_profile().instance_name152 Name = self.getCompName(j)153 if Name == self.rs[h].ar[i].name:154 self.rs[h].ar[i].r = self._worker._comps[j]155 for i in range(0, len(self.rs[h].rs)):156 for j in range(0, len(self.rs[h].rs[i].SR)):157 for k in range(0, len(self.rs[h].rs[i].SR[j])):158 159 for l in range(0, len(self._worker._comps)):160 #Name = self._worker._comps[l]._sm._obj.get_component_profile().instance_name161 Name = self.getCompName(l)162 if Name == self.rs[h].rs[i].SR[j][k].v:163 164 self.rs[h].rs[i].SR[j][k].r = self._worker._comps[l]165 166 167 ##168 # @brief GUIから実行順序の読み込みの関数169 # @param self170 # @param RS_d 実行順序のリスト171 def LoadRuleGUI(self, RS_d):172 guard = OpenRTM_aist.ScopedLock(self._workerthread._mutex)173 self.rs = []174 self.rs = RS_d175 176 177 self.LoadRules()178 del guard179 180 181 ##182 # @brief ファイルから実行順序の読み込みの関数183 # @param self184 def LoadRule(self):185 186 187 guard = OpenRTM_aist.ScopedLock(self._workerthread._mutex)188 189 190 191 192 for h in range(0, len(self.rs)):193 self.rs[h].rs = []194 self.rs = []195 MPComp.LoadMainRule(self.rs, self.FileName)196 self.LoadRules()197 198 del guard199 200 ##201 # @brief スレッド実行関数202 # @param self203 # @return 0204 def svc(self):205 self._rtcout.RTC_TRACE("svc()")206 #flag = True207 count_ = 0208 self.LoadRule()209 210 211 212 #while flag:213 while self.threadRunning():214 215 guard = OpenRTM_aist.ScopedLock(self._workerthread._mutex)216 #self.LoadRules()217 #self._worker._cond.acquire()218 #while not self._worker._running:219 # self._worker._cond.wait()220 221 while not self._workerthread._running:222 self._workerthread._cond.wait()223 self._worker.updateComponentList()224 t0_ = OpenRTM_aist.Time()225 if self._worker._running:226 227 for i in range(0, len(self.rs)):228 S = True229 for j in range(0, len(self.rs[i].ar)):230 231 Flag = False232 for k in range(0, len(self._worker._comps)):233 if self.rs[i].ar[j].r == self._worker._comps[k]:234 235 if self.rs[i].ar[j].state == -1:236 237 pass238 else:239 if self.rs[i].ar[j].state != self._worker._comps[k]._sm.get_state():240 S = False241 if Flag == False:242 for k in range(0, len(self._worker._comps)):243 if self.getCompName(k) == self.rs[i].ar[j].name:244 self.rs[i].ar[j].r = self._worker._comps[k]245 if self.rs[i].ar[j].state == -1:246 pass247 else:248 if self.rs[i].ar[j].state != self._worker._comps[k]._sm.get_state():249 S = False250 if S == True:251 self.r_num = i252 break253 254 if self.r_num < len(self.rs):255 256 for i in range(0, len(self.rs[self.r_num].rs)):257 258 if len(self.rs[self.r_num].rs[i].SR) == 0:259 pass260 elif len(self.rs[self.r_num].rs[i].SR) == 1:261 262 for j in range(0, len(self.rs[self.r_num].rs[i].SR[0])):263 self.rs[self.r_num].rs[i].SR[0][j].s = 1264 """sd = self.rs[self.r_num].rs[i].SR[0][j].r in self._worker._comps265 print self.rs[self.r_num].rs[i].SR[0][j].v,self.rs[self.r_num].rs[i].SR[0][j].r266 if sd == True:267 self.rs[self.r_num].rs[i].SR[0][j].r._sm.worker()"""268 self.workerComp(self.rs[self.r_num].rs[i].SR[0][j])269 self.rs[self.r_num].rs[i].SR[0][j].s = 0270 else:271 272 p_num = len(self.rs[self.r_num].rs[i].SR)273 m_task = []274 for j in range(0, p_num):275 m_task_s = MPTask(self)276 m_task.append(m_task_s)277 for k in range(0, len(self.rs[self.r_num].rs[i].SR[j])):278 m_task_s.addComp(self.rs[self.r_num].rs[i].SR[j][k],i,j,k)279 m_task_s.activate()280 for j in range(0, p_num):281 m_task[j].wait()282 m_task[j].close()283 284 285 286 #self._worker._cond.release()287 del guard288 t1_ = OpenRTM_aist.Time()289 period_ = self.getPeriod()290 if count_ > 1000:291 exctm_ = (t1_ - t0_).getTime().toDouble()292 #slptm_ = self._period.toDouble() - exctm_293 slptm_ = period_.toDouble() - exctm_294 self._rtcout.RTC_PARANOID("Period: %f [s]", period_.toDouble())295 self._rtcout.RTC_PARANOID("Execution: %f [s]", exctm_)296 self._rtcout.RTC_PARANOID("Sleep: %f [s]", slptm_)297 t2_ = OpenRTM_aist.Time()298 if not self._nowait and period_.toDouble() > ((t1_ - t0_).getTime().toDouble()):299 if count_ > 1000:300 self._rtcout.RTC_PARANOID("sleeping...")301 slptm_ = period_.toDouble() - (t1_ - t0_).getTime().toDouble()302 time.sleep(slptm_)303 if count_ > 1000:304 t3_ = OpenRTM_aist.Time()305 self._rtcout.RTC_PARANOID("Slept: %f [s]", (t3_ - t2_).getTime().toDouble())306 count_ = 0307 count_ += 1308 #flag = self._running309 310 return 0311 312 313##314# @brief 実行コンテキスト初期化関数315# @param manager マネージャオブジェクト316def MultipleOrderedECInit(manager):317 318 OpenRTM_aist.ExecutionContextFactory.instance().addFactory("MultipleOrderedEC",319 MultipleOrderedEC,320 OpenRTM_aist.ECDelete)...

Full Screen

Full Screen

progress_dialog.py

Source:progress_dialog.py Github

copy

Full Screen

1# -*- coding: utf-8 -*-2#3# Copyright (C) 2014 GNS3 Technologies Inc.4#5# This program is free software: you can redistribute it and/or modify6# it under the terms of the GNU General Public License as published by7# the Free Software Foundation, either version 3 of the License, or8# (at your option) any later version.9#10# This program is distributed in the hope that it will be useful,11# but WITHOUT ANY WARRANTY; without even the implied warranty of12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the13# GNU General Public License for more details.14#15# You should have received a copy of the GNU General Public License16# along with this program. If not, see <http://www.gnu.org/licenses/>.17"""18Progress dialog that blocking tasks (file operations, network connections etc.)19"""20from ..qt import sip21from gns3.version import __version__22from ..qt import QtWidgets, QtCore, qslot23import logging24log = logging.getLogger(__name__)25class ProgressDialog(QtWidgets.QProgressDialog):26 """27 Progress dialog implementation with thread support.28 :param thread: thread to run29 :param title: window title30 :param label_text: text to describe the progress bar31 :param cancel_button_text: text for the cancel button32 :param busy: if True, the progress bar in "sliding mode"33 :param delay: Countdown in seconds before starting the worker34 :param create_thread: Start the worker in a dedicated thread35 to show unknown progress.36 :param parent: parent widget37 """38 def __init__(self, worker, title, label_text, cancel_button_text, busy=False, parent=None, delay=0, create_thread=True, cancelable=False):39 if "dev" in __version__:40 assert QtCore.QThread.currentThread() == QtWidgets.QApplication.instance().thread()41 minimum = 042 maximum = 10043 if busy:44 maximum = 045 super().__init__(label_text, cancel_button_text, minimum, maximum, parent)46 self.setModal(True)47 self.setAttribute(QtCore.Qt.WA_DeleteOnClose, True)48 self._errors = []49 self.setWindowTitle(title)50 self.canceled.connect(self._canceledSlot)51 self.destroyed.connect(self._cleanup)52 self._cancelable = cancelable53 self._worker = worker54 self._worker.setObjectName(worker.__class__.__name__)55 if create_thread:56 self._thread = QtCore.QThread()57 if "dev" in __version__:58 assert not self._worker.parent()59 log.debug("Set worker inside a thread {}".format(self._worker.__class__))60 self._worker.moveToThread(self._thread)61 else:62 log.debug("Set worker outside of a thread {}".format(self._worker.__class__))63 self._thread = None64 self._worker.finished.connect(self.accept)65 self._worker.updated.connect(self._updateProgressSlot)66 self._worker.error.connect(self._error)67 if self._thread:68 self._thread.started.connect(self._worker.run)69 self._countdownTimer = None70 if delay == 0:71 self._start()72 else:73 self._delay = delay74 self._countdownTimer = QtCore.QTimer()75 self._countdownTimer.setInterval(self._delay * 100)76 self._countdownTimer.timeout.connect(self._updateCountdownSlot)77 self._countdownTimer.start()78 self._updateCountdownSlot()79 @qslot80 def _updateCountdownSlot(self):81 """82 Called every second for countdown before83 starting the worker84 """85 if self._delay <= 0:86 self.setCancelButtonText("Cancel")87 self._countdownTimer.stop()88 self._start()89 else:90 self.setCancelButtonText("Cancel start ({} seconds)".format(self._delay))91 self._delay -= 192 def _start(self):93 # connect the thread signals and start the thread94 if self._thread:95 self._thread.start()96 log.debug("{} thread started".format(self._worker.objectName()))97 elif self._worker:98 self._worker.run()99 @qslot100 def _canceledSlot(self):101 if self._cancelable and not self._thread:102 self._worker.cancel()103 log.debug("{} worker canceled".format(self._worker.objectName()))104 if self._thread:105 self._worker.cancel()106 log.debug("{} thread canceled".format(self._worker.objectName()))107 self._cleanup()108 @qslot109 def accept(self):110 if self._worker:111 log.debug("{} thread finished".format(self._worker.objectName()))112 self._cleanup()113 super().accept()114 def __del__(self):115 self._cleanup()116 @qslot117 def _cleanup(self):118 """119 Delete the thread.120 """121 if self._countdownTimer:122 self._countdownTimer.stop()123 if self._thread and not sip.isdeleted(self._thread):124 if self._thread.isRunning():125 log.debug("{} thread is being destroyed".format(self._worker.objectName()))126 thread = self._thread127 self._thread = None128 thread.quit()129 if not thread.wait(3000):130 thread.terminate()131 thread.wait()132 log.debug("{} thread destroyed".format(self._worker.objectName()))133 thread.deleteLater()134 self._worker = None135 @qslot136 def _updateProgressSlot(self, value):137 """138 Slot to update the progress bar value.139 :param value: value for the progress bar (integer)140 """141 if self._thread:142 # It seems in some cases this is called on a deleted object and crash143 self.setValue(value)144 @qslot145 def _error(self, message, stop=False):146 """147 Slot to show an error message sent by the thread.148 :param message: message149 """150 if stop:151 log.critical("{} thread stopping with an error: {}".format(self._worker.objectName(), message))152 self._canceledSlot()153 QtWidgets.QMessageBox.critical(self.parentWidget(), "Error", "{}".format(message))154 else:155 self._errors.append(message)156 self._cleanup()157 def errors(self):158 """159 Returns error messages.160 :returns: error message list161 """...

Full Screen

Full Screen

testWorker.py

Source:testWorker.py Github

copy

Full Screen

1#!/usr/bin/env python2# -*- coding: utf-8 -*-3__author__ = 'Leon'4from unittest import TestCase5from loongtian.util.tasks.tasksManager import *6from loongtian.util.tasks.task import Task,WorkRequest7import loongtian.util.tasks.queue as Queue8class TestWorker(TestCase):9 def setUp(self):10 print ("----setUp----")11 pass # def setUp(self):12 def testWorkerRequest(self):13 print(123)14 print ("----testWorkerRequest测试WorkRequest工作项(加载的是函数)----")15 import random16 import time,datetime17 #测试用函数18 def do_work(data):19 time.sleep(2)20 return data+121 #返回结果调用callback22 def print_result(request,result):23 print ("---Result from request id: %s data: %r time:%s" % (request.id,result,datetime.datetime.now()))24 _worker = TasksManager(3, 'worker1', 1, auto_start =False)25 for i in range(20):#先创建20个任务26 req = WorkRequest(do_work,args=[i],kwds={},requestID=i,callback=print_result)27 _worker.addTask(req)28 print ("work request #%s added." % req.id)29 print ('-'*20,'workmanager.threadWorkerSize:', _worker.threadWorkerSize(), '-' * 20)30 #开始执行所有的工作请求31 _worker.start()32 print ('-'*20,'after start workmanager.threadWorkerSize:', _worker.threadWorkerSize(), '-' * 20)33 counter = 034 while True:35 try:36 time.sleep(0.5)37 _worker.poll()38 if(counter==5):39 print ("Add 3 more __threadWorkers threads")40 _worker.createThreadWorkers(3)41 print ('-'*20,'after Add 3 more,workmanager.threadWorkerSize:', _worker.threadWorkerSize(), '-' * 20)42 if(counter==10):43 print ("dismiss 2 __threadWorkers threads")44 _worker.stopThreadWorkers(2)45 print ('-'*20,'after dismiss 2,workmanager.threadWorkerSize:', _worker.threadWorkerSize(), '-' * 20)46 if (counter==15):47 print ("Add 10 more WorkRequest.")48 for i in range(20,30):#再创建10个任务49 req = WorkRequest(do_work,args=[i],kwds={},requestID=i,callback=print_result)50 print ("work request #%s added." % req.id)51 _worker.addTask(req)52 print (_worker.getThreadWorkersStates())53 time.sleep(2)54 counter+=155 except NoResultsPending:56 print ("no pending results")57 break58 except Queue.Empty:59 print ('queue empty')60 break61 _worker.stop()62 print ("Stop")63 pass # def testWorkerRequest(self)64 def testWorkerTask(self):65 print ("----testWorkerTask测试Task工作项(加载的是重载的Task类)----")66 #测试用类67 class ComputationTask(Task):68 def __init__(self,id):69 Task.__init__(self,id,id)70 self.inp=id71 # self.callback=print_result72 def start(self):73 time.sleep(0.5) #模拟一个大型程序的耗时74 self.inp += 175 return self.inp76 class InitializeTask(Task):77 def start(self):78 for i in range(200):79 self.workmanager.putTask(ComputationTask, i)80 yield None81 _worker = TasksManager(3, 'worker2', 2, auto_start =False)82 for i in range(50):#先创建20个任务83 task=ComputationTask(i)84 _worker.addTask(task)85 print ("ComputationTask #%s added." % task.id)86 print ('-'*20,'workmanager.threadWorkerSize:', _worker.threadWorkerSize(), '-' * 20)87 #开始执行所有的工作请求88 _worker.start()89 counter = 090 while True:91 try:92 # time.sleep(0.5)93 _worker.poll()94 if(counter==5):95 print ("Add 3 more __threadWorkers threads")96 _worker.createThreadWorkers(3)97 print ('-'*20,'after Add 3 more,workmanager.threadWorkerSize:', _worker.threadWorkerSize(), '-' * 20)98 if(counter==10):99 print ("dismiss 2 __threadWorkers threads")100 _worker.stopThreadWorkers(2)101 print ('-'*20,'after dismiss 2,workmanager.threadWorkerSize:', _worker.threadWorkerSize(), '-' * 20)102 if (counter==15):103 print ("Add 10 more ComputationTask.")104 for i in range(50,60):#再创建10个任务105 task=ComputationTask(i)106 _worker.addTask(task)107 print ("ComputationTask #%s added." % task.id)108 time.sleep(2)109 counter+=1110 except NoResultsPending:111 print ("no pending results")112 break113 except Queue.Empty:114 print ('queue empty')115 break116 _worker.stop()117 print ("Stop")118 def tearDown(self):119 print ("----tearDown----")...

Full Screen

Full Screen

parallel.py

Source:parallel.py Github

copy

Full Screen

...5__all__ = ['Worker', 'WorkerManager']6class WorkerManager:7 def __init__(self):8 self._manager = mp.Manager()9 self._worker = self.spawn_worker()10 def __del__(self):11 self.disconnect()12 def connect(self):13 self.start_worker()14 while not self._worker.isalive:15 time.sleep(0.01)16 def disconnect(self):17 self.kill_worker()18 #################19 def spawn_worker(self):20 return NotImplemented21 def start_worker(self):22 if self._worker is None:23 self._worker = self.spawn_worker()24 if not self._worker.isalive:25 self._worker.start()26 return 027 def kill_worker(self):28 if self._worker is None:29 return 030 if self._worker.isalive:31 try:32 self._worker.job_Q.put(None)33 except FileNotFoundError:34 sayd('During killing worker, file not found error.')35 except EOFError:36 sayd('During killing worker, EOFError.')37 except BrokenPipeError:38 sayd('During killing worker, BrokenPipeError.')39 self._worker.join()40 self._worker = None41 return 042 def reboot_worker(self):43 self.kill_worker()44 self.start_worker()45 def assign_job_worker(self, job):46 self._worker.job_Q.put(job)47 def receive_result_worker(self):48 return self._worker.result_Q.get()49class Worker(mp.Process):50 def __init__(self, manager):51 super().__init__()52 self.job_Q = manager.Queue()53 self.result_Q = manager.Queue()54 self.status = manager.dict()55 self.status['isalive'] = False56 @property57 def isalive(self):58 try:59 return self.status['isalive']60 except:61 return False...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run ATX automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful