def timeout(self,full=False): def test_timeout(c,expr,to): with c: try: # break out of sleep print(expr) c.get('write(2,"tic: "//"%s");%s;write(2,"toc: "//"%s")'%(expr,expr,expr),timeout=to) self.fail('Connection.get("%s") should have timed out.'%expr) except Exc.MDSplusException as e: self.assertEqual(e.__class__,Exc.TdiTIMEOUT) def test_normal(c,expr,**kv): print(expr) c.get(expr,**kv) server,server_port = self._setup_mdsip('ACTION_SERVER', 'ACTION_PORT',7000+self.index,True) svr = svr_log = None try: svr,svr_log = self._start_mdsip(server ,server_port ,'timeout') try: if svr: self.assertEqual(svr.poll(),None) c = Connection(server) test_normal(c,"py('1')") # preload MDSplus on server test_timeout(c,"wait(3)",1000) # break tdi wait test_timeout(c,"py('from time import sleep;sleep(3)')",1500) # break python sleep if full: # timing too unreliable for standard test test_timeout(c,"for(;1;) ;",100) # break tdi inf.loop test_timeout(c,"py('while 1: pass')",500) # break python inf.loop test_normal(c,"1",timeout=1000) if sys.version_info < (3,7): # TODO: make this work under fc29 (python3.7?) test_normal(c,"py('1')",timeout=1000) # verify locks are released self._doTCLTest('stop server %s'%server) finally: self._stop_mdsip((svr,server)) finally: if svr_log: svr_log.close()
def plotdata(path='\\ARCHIVESB::DMD10195:CH1', treename='sandbox', shot=5, server=None): from MDSplus import Connection from pylab import plot, title, xlabel, ylabel if server is not None: Con = Connection(server) Con.openTree(treename, shot) name = Con.get('{}:$NAME'.format(path)) data = Con.get('DATA({})'.format(path)) time = Con.get('DIM_OF({})'.format(path)) unit = Con.get('UNITS_OF({})'.format(path)) else: with Tree(treename, shot) as tree: Node = tree.getNode(path) Data = Node.getData() name = Node.getNode('$NAME').data() data = Data.data() time = Data.dim_of().data() unit = Data.getUnits() plot(time, data) xlabel('time (s)') ylabel(unit) title(name)
def _start_mdsip(self, server, port, logname, protocol=MDSIP_PROTOCOL): if port > 0: from subprocess import Popen, STDOUT logfile = '%s-%s%d.log' % (self.module, logname, self.index) log = open(logfile, 'w') if iswin else None try: hosts = '%s/mdsip.hosts' % self.root params = [ 'mdsip', '-s', '-p', str(port), '-P', protocol, '-h', hosts ] print(' '.join(params + ['&>', logfile])) if not log: params.extend(['2>&1', '|', 'tee', logfile]) mdsip = Popen(params) except: if log: log.close() raise try: self._waitIdle(server, 10) # allow mdsip to launch except Exception: mdsip.kill() raise else: mdsip, log = None, None if server: c = Connection(server) for envpair in self.envx.items(): checkStatus(c.get('setenv($//"="//$)', *envpair)) c.get('tcl($)', 'set verify') return mdsip, log
def download_shot_numbers(shot_numbers, save_prepath, machine, signals): max_cores = machine.max_cores sentinel = -1 fn = partial( save_shot, signals=signals, save_prepath=save_prepath, machine=machine, sentinel=sentinel, ) # can only handle 8 connections at once :( num_cores = min(mp.cpu_count(), max_cores) queue = mp.Queue() # complete_shots = Array('i',zeros(len(shot_numbers)))# = mp.Queue() # mp.queue can't handle larger queues yet! assert len(shot_numbers) < 32000 for shot_num in shot_numbers: queue.put(shot_num) for i in range(num_cores): queue.put(sentinel) connections = [Connection(machine.server) for _ in range(num_cores)] processes = [ mp.Process(target=fn, args=(queue, connections[i])) for i in range(num_cores) ] print("running in parallel on {} processes".format(num_cores)) for p in processes: p.start() for p in processes: p.join()
def run(self, jobset, skip_init): import MDSplus from MDSplus import Connection self.error = [] jobs, names = jobset self.r = {} self.r['error message'] = [''] try: for k, job in enumerate(jobs): for j in job: if j.command == 'connection_mode': if self.connection_str != j.params[0]: self.connection_str = j.params[0] server, port, tree = parse_server_string( self.connection_str) self.use_mdsconnect = (False if server.upper() == 'DIRECT' else True) if self.use_mdsconnect: if self.connection is not None: del self.connection try: if port != '': server = server + ':' + port self.connection = Connection(server) res = 'ok' except: res = ('connection error' + traceback.format_exc()) else: if self.connection is not None: del self.connection self.connection = None res = 'ok' else: res = 'ok' else: if j.command == 'init': if skip_init: continue else: j.command = 'value' if self.use_mdsconnect: res = self._run_job_mdsconnect(j) if len(self.error) != 0: self.r['error message'] = self.error self.r[names[k]] = res return self.r else: res = self._run_job(j) if len(self.error) != 0: self.r['error message'] = self.error self.r[names[k]] = res return self.r self.r[names[k]] = res except: self.error = ['run error', traceback.format_exc()] self.r['error message'] = self.error return self.r
def connectionThreads(self): def requests(c,idx): args = [Float32(i/10+idx) for i in range(10)] for i in range(10): self.assertEqual(c.get("[$,$,$,$,$,$,$,$,$,$]",*args).tolist(),args) c = Connection(self.host) threads = [Thread(name="C%d"%i,target=requests,args=(c,i)) for i in range(10)] for thread in threads: thread.start() for thread in threads: thread.join()
def write(self): count = 100 def thread(test, name, node, count): i = -1 max_period = 0 last = start = time.time() for i in range(count): data = Float32([i * 10 + 1]) dim = Float32([last]) node.makeSegment(dim[0], dim[0], data, dim) end = time.time() max_period = max(end - last, max_period) last = end i += 1 test.assertEqual(i, count) print("%s: rate=%f, max_period=%f" % (name, i / (end - start), max_period)) server, server_port = self._setup_mdsip('ACTION_SERVER', 'ACTION_PORT', 7020 + self.index, True) tempdir = tempfile.mkdtemp() try: svr, svr_log = self._start_mdsip(server, server_port, 'tcp') try: con = Connection(server) def check(line, *args): sts = con.get(line, *args) self.assertTrue(sts & 1, "error %d in '%s'" % (sts, line)) check("setenv('test_path='//$)", tempdir) for line in ( "TreeOpenNew('test', 1)", "TreeAddNode('EV1', _, 6)", "TreeAddNode('EV2', _, 6)", "TreeWrite()", "TreeClose()", ): check(line) setenv("test_path", "%s::%s" % (server, tempdir)) tree = Tree("test", 1) _common.TestThread.assertRun( 100, _common.TestThread('EV1', thread, self, 'EV1', tree.EV1.copy(), count), _common.TestThread('EV2', thread, self, 'EV2', tree.EV2.copy(), count), ) finally: if svr_log: svr_log.close() self._stop_mdsip((svr, server)) finally: shutil.rmtree(tempdir, ignore_errors=False, onerror=None)
def _thread_test(self, server): def requests(self, c, idx): args = [Int32(i + idx + 10) for i in range(10)] for _ in range(20): self.assertEqual( c.get("[$,$,$,$,$,$,$,$,$,$]", *args).tolist(), args) connection = Connection(server) _common.TestThread.assertRun( 100, *(_common.TestThread("T%d" % idx, requests, self, connection, idx) for idx in range(5)))
def loadpaths(host, treename): """Get the path environment variables for a tree being served by a remote system and define them in this process context. The remote system's getpaths function is responsible for fixing up the tree_path values, prefixing hostnames and replacing any shortened hostnames with fully qualified ip address names.""" from MDSplus import Connection import os c = Connection(host) paths = c.get("getpaths('%s')" % treename).deserialize() for i in range(len(paths['path'])): os.environ[paths['tree'][i].rstrip().lower() + '_path'] = paths['path'][i].rstrip()
def io(self): connection = Connection("thread://io") """ mdsconnect """ self.assertEqual(connection.get('_a=1').tolist(), 1) self.assertEqual(connection.get('_a').tolist(), 1) self.assertEqual(connection.getObject('1:3:1').__class__, Range) g = GetMany(connection) g.append('a', '1') g.append('b', '$', 2) g.append('c', '$+$', 1, 2) g.execute() self.assertEqual(g.get('a'), 1) self.assertEqual(g.get('b'), 2) self.assertEqual(g.get('c'), 3)
def threadsLocal(self): c = Connection('local://gub') class ConnectionThread(Thread): def run(self): for i in range(1000): self.test.assertEqual(int(c.get('%d' % i)), i) t1 = ConnectionThread() t1.test = self t2 = ConnectionThread() t2.test = self t1.start() t2.start() t1.join() t2.join()
def threadsTcp(self): server, server_port = self._setup_mdsip('ACTION_SERVER', 'ACTION_PORT', 7100 + self.index, True) svr = svr_log = None try: svr, svr_log = self._start_mdsip(server, server_port, 'tcp') try: if svr is not None: time.sleep(1) def requests(c, idx): args = [Float32(i / 10 + idx) for i in range(10)] for i in range(10): self.assertEqual( c.get("[$,$,$,$,$,$,$,$,$,$]", *args).tolist(), args) c = Connection(server) """ mdsconnect """ self.assertEqual(c.get('_a=1').tolist(), 1) self.assertEqual(c.get('_a').tolist(), 1) self.assertEqual(c.getObject('1:3:1').__class__, Range) g = GetMany(c) g.append('a', '1') g.append('b', '$', 2) g.append('c', '$+$', 1, 2) g.execute() self.assertEqual(g.get('a'), 1) self.assertEqual(g.get('b'), 2) self.assertEqual(g.get('c'), 3) threads = [ Thread(name="C%d" % i, target=requests, args=(c, i)) for i in range(10) ] for thread in threads: thread.start() for thread in threads: thread.join() finally: if svr and svr.poll() is None: svr.terminate() svr.wait() finally: if svr_log: svr_log.close()
#!/usr/bin/env python3 import numpy as np from MDSplus import Connection from scipy.interpolate import interp1d conn = Connection('atlas') # %% shot = 171957 t0 = 3000 def get_bfields(shot, t0=3000, flux_current=False): conn.openTree('efit01', shot) mw = int(conn.get('\\mw')[0]) mh = int(conn.get('\\mh')[0]) time = conn.get('\\gtime') tind = np.argmin(abs(time - t0)) psirz1 = np.array(conn.get(r'\psirz')) r_g = np.array(conn.get(r'\r')) z_g = np.array(conn.get(r'\z')) if flux_current: ssimag = np.array(conn.get(r'\ssimag'))[tind] ssibry = np.array(conn.get(r'\ssibry'))[tind] fpol1 = np.array(conn.get(r'\fpol')) fpol = fpol1[tind, :] bcentr = np.array(conn.get(r'\bcentr'))[tind] rbcent = np.array(conn.get(r'\rbcent'))[tind] conn.closeAllTrees()
def get_connection(self): return Connection(self.server)
start = time() d = c.Data.execute('random($)', samples).data() end = time() print( """ Speed test for connection to %s Using dill for pickling: %s Floating point samples retrieved: %d MBytes received: %g Transaction time in seconds: %g Transfer speed in MB/s: %g """ % (host, usingDill, samples, d.nbytes * 1E-6, end - start, d.nbytes * 1E-6 / (end - start))) c.connection.close() if mdsip is not None: from MDSplus import Connection c = Connection(mdsip) start = time() d = c.get('random($)', samples).data() end = time() print(""" Speed test for mdsip connection to %s Floating point samples retrieved: %d MBytes received: %g Transaction time in seconds: %g Transfer speed in MB/s: %g """ % (mdsip, samples, d.nbytes * 1E-6, end - start, d.nbytes * 1E-6 / (end - start)))
def run(self, jobset, skip_init): import MDSplus from MDSplus import Connection self.error = [] jobs, names = jobset self.r = {'error message': []} sig_names = [] dim_names = [] try: for k, job in enumerate(jobs): for j in job: if j.command == 'connection_mode': # print 'connection_mode param', self.connection_str, j.params, self.connection # if self.connection is not None: # del self.connection # self.connection = None if self.connection_str != j.params[0]: # if True: server, port, tree = parse_server_string( j.params[0]) self.use_mdsconnect = (False if server.upper() == 'DIRECT' else True) if self.use_mdsconnect: if self.connection is not None: del self.connection self.connection = None try: if port != '': server = server + ':' + port self.connection = Connection(server) self.connection_str = j.params[0] res = 'ok' except: self.r['error message'] = [ 'connection error' + traceback.format_exc() ] self.r[names[k]] = None return self.r else: if self.connection is not None: del self.connection self.connection = None res = 'ok' self._connection_flag = True else: res = 'ok' else: if j.command == 'init': if skip_init: continue else: j.command = 'value' if self.use_mdsconnect: # res = self._run_job_mdsconnect(j, sig_names, dim_names, names[k]) res = self._run_job_mdsconnect(j) if len(self.error) != 0: self.r['error message'] = self.error[:] self.r[names[k]] = res return self.r # return _evaluate_sig(self.r, sig_names, dim_names) else: res = self._run_job(j) if len(self.error) != 0: self.r['error message'] = self.error[:] self.r[names[k]] = res return self.r self._connection_flag = False # return _evaluate_sig(self.r, sig_names, dim_names) self.r[names[k]] = res except: self.error = ['run error', traceback.format_exc()] # self.r = _evaluate_sig(self.r, sig_names, dim_names) self.r['error message'] = self.error return self.r
class ForkingTCPRequestHandler(SocketServer.StreamRequestHandler): # def __init__(self, *args, **kargs): name = '/tmp/mdsplus_gateway_'+str(os.getpid()) +'.log' self.use_mdsconnect = False self.connection_str = '' self.connection = None if write_handler_log: self.fid = open(name, 'w') else: self.fid = None SocketServer.StreamRequestHandler.__init__(self, *args, **kargs) def write_log(self, txt): if self.fid is not None: self.fid.write(txt + '\n') def handle(self): try: while True: self.loop_handler() except: pass def loop_handler(self): message = self.rfile.readline().strip() if self.fid is not None: self.fid.write('handling input \n' + message + '\n') asmessage = binascii.a2b_hex(message) # logging.basicConfig(level = logging.DEBUG) try: data = pickle.loads(asmessage) except ValueError, EOFError: if self.fid is not None: self.fid.write('picke error \n') self.fid.flush() # logging.exception('picke load error', len(message)) # response = None # return #cur_thread = threading.current_thread() if self.fid is not None: self.fid.write('handling request \n' + str(data) + '\n') self.fid.flush() r = {} for name, commands in data: for command in commands: com = command[0] param = command[1:] #cur_thread = threading.current_thread() if self.fid is not None: self.fid.write(com + ':' + param + '\n') self.fid.flush() if com == 's': server, port, tree = parse_server_string(param) self.use_mdsconnect = (False if server.upper() == 'DIRECT' else True) if self.use_mdsconnect: try: if self.connection_str != param: del self.connection if port != '': server = server + ':' + port self.connection = Connection(server) self.connection_str = param r[name] = 'ok' except: r[name] = 'connection error' self.error = ['connection error', self.connection_str] else: if self.connection is not None: del self.connection self.connection = None self.connection_str = '' r[name] = 'ok' else: self.error = [] if self.use_mdsconnect: r[name] = self._handle_mdsconnect(com, param) else: r[name] = self._handle(com, param) if len(self.error) != 0: if self.fid is not None: self.fid.write(str(self.error)) self.fid.flush() if self.fid is not None: self.fid.write('return variables \n' + str(r.keys()) + '\n') self.fid.flush() sr=pickle.dumps(r) response = binascii.b2a_hex(sr) if self.fid is not None: self.fid.write('sending data (length) \n' + str(len(response)) + '\n') self.fid.flush() self.request.sendall(response+'\n') if self.fid is not None: self.fid.flush()
def thick(self): def testnci(thick, local, con, nci): l = local.S.__getattribute__(nci) t = thick.S.__getattribute__(nci) if nci.endswith("_nids"): l, t = str(l), str(t) try: c = str(con.get("getnci(getnci(S,$),'nid_number')", nci)) except TreeNNF: c = '[]' else: c = con.get("getnci(S,$)", nci) try: self.assertEqual(t, c) self.assertEqual(t, l) except: print(nci, t, l, c) raise server, server_port = self._setup_mdsip('ACTION_SERVER', 'ACTION_PORT', 7000 + self.index, True) svr, svr_log = self._start_mdsip(server, server_port, 'thick') try: con = Connection(server) self.assertEqual( con.get("zero([1,1,1,1,1,1,1,1],1)").tolist(), [[[[[[[[0]]]]]]]]) with Tree(self.tree, -1, "new") as local: local.addNode(self.treesub, "SUBTREE") s = local.addNode("S", "SIGNAL") s.addTag("tagS") s.record = ADD(Float32(1), Float32(2)) t = local.addNode("T", "TEXT") t.addNode("TT", "TEXT").addTag("tagTT") t.record = t.TT t.TT = "recTT" local.write() with Tree(self.treesub, -1, "new") as sub: sub.addNode("OK") sub.write() local.normal() Tree.setCurrent(self.tree, 7) setenv("%s_path" % self.tree, "%s::" % server) print(con.get("getenv($//'_path')", self.tree)) con.get("TreeShr->TreeOpen(ref($),val($),val(1))", self.tree, -1) thick = Tree(self.tree, -1) thick.createPulse(1) thick1 = Tree(self.tree, 1) self.assertEqual( getattr(local, self.treesub.upper()).OK.nid, getattr(thick1, self.treesub.upper()).OK.nid) local_filename = local.getFileName() thick_filename = thick.getFileName() self.assertTrue("::" in thick_filename, thick_filename) self.assertTrue(local_filename, thick_filename.split("::", 1)[1]) """ TreeTurnOff / TreeTurnOn """ thick.S.on = False self.assertEqual(local.S.on, False) thick.S.on = True self.assertEqual(local.S.on, True) """ TreeSetCurrentShotId / TreeGetCurrentShotId """ Tree.setCurrent(self.tree, 1) self.assertEqual(Tree.getCurrent(self.tree), 1) """ TreeGetRecord / TreeSetRecord """ self.assertEqual(str(local.S.record), "1. + 2.") self.assertEqual(str(thick.S.record), "1. + 2.") thick.S.record = ADD(Float32(2), Float32(4)) self.assertEqual(str(local.S.record), "2. + 4.") self.assertEqual(str(thick.S.record), "2. + 4.") self.assertEqual(str(local.T.record), str(thick.T.record)) """ GetDefaultNid / SetDefaultNid """ self.assertEqual(thick.getDefault(), thick.top) thick.setDefault(thick.S) self.assertEqual(thick.getDefault(), thick.top.S) thick.setDefault(thick.top) """ FindNodeWildRemote """ self.assertEqual(str(thick.getNodeWild("T*")), str(local.getNodeWild("T*"))) """ FindTagWildRemote """ self.assertEqual(thick.findTags("*"), local.findTags("*")) """ nci """ thick.S.write_once = True self.assertEqual(thick.S.write_once, True) for nci in ( 'on', 'depth', 'usage_str', 'dtype', 'length', 'rlength', 'fullpath', 'minpath', 'member_nids', 'children_nids', 'rfa', 'write_once', ): testnci(thick, local, con, nci) """ new stuff """ self.assertEqual(local.getFileName(), con.get("treefilename($,-1)", self.tree)) finally: if svr_log: svr_log.close() self._stop_mdsip((svr, server))
import os import cv2 import time import numpy as np import pandas as pd import matplotlib.pyplot as plt from MANTIS import MANITS_Camera from MDSplus import Connection ## >> ssh -NT4 [email protected] -L 5555:mantis4:8000 ## in a terminal con = Connection('localhost:5555') meta = pd.read_csv("meta.csv") # Setting up the resizing parameters IMG_HEIGHT = 128 IMG_WIDTH = 128 # We create a single black frame that we can concatenate on later all_shots = np.zeros((1, 128, 128, 1)) all_labels = [0] frames_per_shot = {} for index, row in meta.iterrows(): if (index != 23): # This video was wrongfully IDed print(row["shot number"]) counter = 0 for j in range(1, 11): print("Working on channel ", j) # Upload the footage # There is no footage for these sepecific cameras
def __init__(self, shot='-1', tree='W7X', server='mds-data-1'): from MDSplus import Connection self._tree = tree self._shot = shot self._connection = Connection(server) super(remoteTree, self).__init__(tree, shot)
def run(self, jobset, skip_init): # def _evaluate_sig(res, sig_names, dim_names): # for name in dim_names: # try: # src, dim = res[name][0:3].split(',') # res[name] = res[src].dim_of(long(dim)).data() # except: # pass # for name in sig_names: # try: # res[name] = res[name].data() # except: # pass # return res self.error = [] jobs, names = jobset self.r = {'error message': []} sig_names = [] dim_names = [] try: for k, job in enumerate(jobs): for j in job: # print j if j.command == 'connection_mode': # print 'connection_mode param', self.connection_str, j.params, self.connection # if self.connection is not None: # del self.connection # self.connection = None if self.connection_str != j.params[0]: # if True: server, port, tree = parse_server_string( j.params[0]) self.use_mdsconnect = (False if server.upper() == 'DIRECT' else True) if self.use_mdsconnect: if self.connection is not None: del self.connection self.connection = None try: if port != '': server = server + ':' + port self.connection = Connection(server) self.connection_str = j.params[0] res = 'ok' except: self.r['error message'] = [ 'connection error' + traceback.format_exc() ] self.r[names[k]] = None return self.r else: if self.connection is not None: del self.connection self.connection = None res = 'ok' else: res = 'ok' self._connection_flag = True else: if j.command == 'init': if skip_init: continue else: j.command = 'value' if self.use_mdsconnect: # res = self._run_job_mdsconnect(j, sig_names, dim_names, names[k]) #ttt = time.time() res = self._run_job_mdsconnect(j) #print time.time() - ttt, j if len(self.error) != 0: self.r['error message'] = self.error[:] self.r[names[k]] = res return self.r # return _evaluate_sig(self.r, sig_names, dim_names) else: res = self._run_job(j) if len(self.error) != 0: self.r['error message'] = self.error[:] self.r[names[k]] = res return self.r # return _evaluate_sig(self.r, sig_names, dim_names) self._connection_flag = False self.r[names[k]] = res except: self.error = ['run error', traceback.format_exc()] # self.r = _evaluate_sig(self.r, sig_names, dim_names) self.r['error message'] = self.error return self.r
def test(): from time import sleep hosts = '%s/mdsip.hosts' % self.root def testDispatchCommand(mdsip, command, stdout=None, stderr=None): self.assertEqual( tcl( 'dispatch/command/nowait/server=%s %s' % (mdsip, command), 1, 1, 1), (None, None)) def setup_mdsip(server_env, port_env, default_port, fix0): host = getenv(server_env, '') if len(host) > 0: return host, 0 port = int(getenv(port_env, default_port)) if port == 0: if fix0: port = default_port else: return None, 0 return 'localhost:%d' % (port, ), port def start_mdsip(server, port, logname, env=None): if port > 0: from subprocess import Popen, STDOUT logfile = '%s_%d.log' % (logname, self.index) log = open(logfile, 'w') try: params = ['mdsip', '-s', '-p', str(port), '-h', hosts] print(' '.join(params + ['&>', logfile])) mdsip = Popen(params, env=env, stdout=log, stderr=STDOUT) except: log.close() raise return mdsip, log if server: for envpair in self.envx.items(): testDispatchCommand(server, 'env %s=%s' % envpair) return None, None monitor, monitor_port = setup_mdsip('ACTION_MONITOR', 'MONITOR_PORT', 8700 + self.index, False) monitor_opt = "/monitor=%s" % monitor if monitor_port > 0 else "" server, server_port = setup_mdsip('ACTION_SERVER', 'ACTION_PORT', 8800 + self.index, True) shot = self.shot + 1 Tree('pytree', -1, 'ReadOnly').createPulse(shot) show_server = "Checking server: %s\n[^,]+, [^,]+, logging enabled, Inactive\n" % server pytree = Tree('pytree', shot) pytree.TESTDEVICE.ACTIONSERVER.no_write_shot = False pytree.TESTDEVICE.ACTIONSERVER.record = server """ using dispatcher """ mon, mon_log, svr, svr_log = (None, None, None, None) try: mon, mon_log = start_mdsip(monitor, monitor_port, 'monitor') svr, svr_log = start_mdsip(server, server_port, 'server', self.env) try: sleep(3) if mon: self.assertEqual(mon.poll(), None) if svr: self.assertEqual(svr.poll(), None) """ mdsconnect """ c = Connection(server) self.assertEqual(c.get('1').tolist(), 1) self.assertEqual(c.getObject('1:3:1').__class__, Range) if not sys.platform.startswith( 'win'): # Windows does not support timeout yet try: # currently the connection needs to be closed after a timeout Connection(server).get("wait(1)", timeout=100) self.fail( "Connection.get(wait(1)) should have timed out." ) except Exc.MDSplusException as e: self.assertEqual(e.__class__, Exc.TdiTIMEOUT) g = GetMany(c) g.append('a', '1') g.append('b', '$', 2) g.append('c', '$+$', 1, 2) g.execute() self.assertEqual(g.get('a'), 1) self.assertEqual(g.get('b'), 2) self.assertEqual(g.get('c'), 3) """ tcl dispatch """ self._doTCLTest('show server %s' % server, out=show_server, re=True) testDispatchCommand(server, 'set verify') testDispatchCommand(server, 'type test') self._doTCLTest('set tree pytree/shot=%d' % shot) self._doTCLTest('dispatch/build%s' % monitor_opt) self._doTCLTest('dispatch/phase%s INIT' % monitor_opt) sleep(1) self._doTCLTest('show server %s' % server, out=show_server, re=True) self._doTCLTest('dispatch/phase%s PULSE' % monitor_opt) sleep(1) self._doTCLTest('show server %s' % server, out=show_server, re=True) self._doTCLTest('dispatch/phase%s STORE' % monitor_opt) sleep(1) self._doTCLTest('show server %s' % server, out=show_server, re=True) """ tcl exceptions """ self._doExceptionTest( 'dispatch/command/server=%s ' % server, Exc.MdsdclIVVERB) """ tcl check if still alive """ if mon: self.assertEqual(mon.poll(), None) if svr: self.assertEqual(svr.poll(), None) finally: try: self._doTCLTest( 'dispatch/command/wait/server=%s close/all' % server) finally: if svr and svr.poll() is None: svr.terminate() svr.wait() if mon and mon.poll() is None: mon.terminate() mon.wait() finally: if svr_log: svr_log.close() if mon_log: mon_log.close() self._doTCLTest('close/all') pytree = Tree('pytree', shot, 'ReadOnly') self.assertTrue(pytree.TESTDEVICE.INIT1_DONE.record <= pytree.TESTDEVICE.INIT2_DONE.record)
def loop_handler(self): message = self.rfile.readline().strip() if self.fid is not None: self.fid.write('handling input \n' + message + '\n') asmessage = binascii.a2b_hex(message) # logging.basicConfig(level = logging.DEBUG) try: data = pickle.loads(asmessage) except ValueError as EOFError: if self.fid is not None: self.fid.write('picke error \n') self.fid.flush() # logging.exception('picke load error', len(message)) # response = None # return #cur_thread = threading.current_thread() if self.fid is not None: self.fid.write('handling request \n' + str(data) + '\n') self.fid.flush() r = {} for name, commands in data: for command in commands: com = command[0] param = command[1:] #cur_thread = threading.current_thread() if self.fid is not None: self.fid.write(com + ':' + param + '\n') self.fid.flush() if com == 's': server, port, tree = parse_server_string(param) self.use_mdsconnect = (False if server.upper() == 'DIRECT' else True) if self.use_mdsconnect: try: if self.connection_str != param: del self.connection if port != '': server = server + ':' + port self.connection = Connection(server) self.connection_str = param r[name] = 'ok' except: r[name] = 'connection error' self.error = [ 'connection error', self.connection_str ] else: if self.connection is not None: del self.connection self.connection = None self.connection_str = '' r[name] = 'ok' else: self.error = [] if self.use_mdsconnect: r[name] = self._handle_mdsconnect(com, param) else: r[name] = self._handle(com, param) if len(self.error) != 0: if self.fid is not None: self.fid.write(str(self.error)) self.fid.flush() if self.fid is not None: self.fid.write('return variables \n' + str(list(r.keys())) + '\n') self.fid.flush() sr = pickle.dumps(r) response = binascii.b2a_hex(sr) if self.fid is not None: self.fid.write('sending data (length) \n' + str(len(response)) + '\n') self.fid.flush() self.request.sendall(response + '\n') if self.fid is not None: self.fid.flush()
class EventListener(threading.Thread): def __init__(self, *args, **kargs): super(EventListener, self).__init__(*args, **kargs) self.queue = mp.Queue() # queue to receive message self.task_queue = mp.JoinableQueue() self.listener_proc = EventListenerProc(self.task_queue, self.queue) self.listener_proc.start() def run(self, *args, **kargs): while True: event_name = self.queue.get(True) if event_name == 'stop': #globals()['listener_thread'] = None return mds_event_listener_lock.acquire() check = False # do nothing mds_event_listener_lock.release() if __name__ == '__main__': server = 'ssh://[email protected]' # this makes a dead-lokc # server = 'alcdata.psfc.mit.edu' # this works c = Connection(server) listener_thread = EventListener() listener_thread.task_queue.put(('exit', ''), False) listener_thread.queue.put('stop') print('good bye')