Skip to content
Snippets Groups Projects
Commit 2938666b authored by Francois Henrotte's avatar Francois Henrotte
Browse files

python-onelab with subclients

parent 187fcb36
No related branches found
No related tags found
No related merge requests found
...@@ -78,6 +78,7 @@ class client : ...@@ -78,6 +78,7 @@ class client :
_GMSH_CONNECT = 27 _GMSH_CONNECT = 27
_GMSH_OLPARSE = 28 _GMSH_OLPARSE = 28
_GMSH_PARAM_NOT_FOUND = 29 _GMSH_PARAM_NOT_FOUND = 29
_GMSH_OPTION_1 = 100
def _receive(self) : def _receive(self) :
def buffered_receive(l) : def buffered_receive(l) :
...@@ -97,8 +98,13 @@ class client : ...@@ -97,8 +98,13 @@ class client :
def _send(self, t, msg) : def _send(self, t, msg) :
m = msg.encode('utf-8') m = msg.encode('utf-8')
try:
if self.socket.send(struct.pack('ii%is' %len(m), t, len(m), m)) == 0 : if self.socket.send(struct.pack('ii%is' %len(m), t, len(m), m)) == 0 :
RuntimeError('onelab socket closed') RuntimeError('onelab socket closed')
except socket.error:
self.socket.close()
self._createSocket()
self.socket.send(struct.pack('ii%is' %len(m), t, len(m), m))
def _def_parameter(self, param) : def _def_parameter(self, param) :
if not self.socket : if not self.socket :
...@@ -145,16 +151,22 @@ class client : ...@@ -145,16 +151,22 @@ class client :
def sub_client(self, name, command): def sub_client(self, name, command):
print 'Defining the subclient ' + name print 'Defining the subclient ' + name
msg = [name, command] msg = [name, command]
## msg.append(name + '\0')
## msg.append(command + '\0')
if not self.socket : if not self.socket :
return return
self._send(self._GMSH_CONNECT, '\0'.join(msg)) self._send(self._GMSH_CONNECT, '\0'.join(msg))
## (t, msg) = self._receive()
## print ("python receive : ", t, msg)
## if t == self._GMSH_CONNECT and msg :
## print "The client %s is now connected" %(msg)
## print "python launch : "+ command + " -onelab " + name + " " + msg
## os.system(command + " -onelab " + name + " " + msg)
def wait_on_subclient(self, name):
if not self.socket :
return
(t, msg) = self._receive() (t, msg) = self._receive()
print ("python receive : ", t, msg) if t == self._GMSH_OPTION_1 :
if t == self._GMSH_CONNECT and msg : print 'Client <%s> done, proceeding with the script...' %(name)
print "python launch : "+ command + " -onelab " + name + " " + msg
os.system(command + " -onelab " + name + " " + msg)
def merge_file(self, filename) : def merge_file(self, filename) :
if not self.socket : if not self.socket :
...@@ -170,19 +182,26 @@ class client : ...@@ -170,19 +182,26 @@ class client :
filename = os.getcwd() + "/" + filename; filename = os.getcwd() + "/" + filename;
self._send(self._GMSH_OLPARSE, filename) self._send(self._GMSH_OLPARSE, filename)
def _createSocket(self) :
addr = self.addr
if '/' in addr or '\\' in addr or ':' not in addr :
self.socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
else :
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.socket.connect(addr)
#self.socket.setblocking(1)
#self.socket.settimeout(5.0)
def __init__(self): def __init__(self):
self.socket = None self.socket = None
self.name = "" self.name = ""
self.addr = ""
for i, v in enumerate(sys.argv) : for i, v in enumerate(sys.argv) :
if v == '-onelab': if v == '-onelab':
self.name = sys.argv[i + 1] self.name = sys.argv[i + 1]
addr = sys.argv[2] self.addr = sys.argv[i + 2]
if '/' in addr or '\\' in addr or ':' not in addr : self._createSocket()
self.socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) self._send(self._GMSH_START, str(os.getpid()))
else :
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.socket.connect(sys.argv[i + 2])
self._send(self._GMSH_START, str(os.getpid))
self.action = self.get_string('python/Action') self.action = self.get_string('python/Action')
if self.action == "initialize": exit(0) if self.action == "initialize": exit(0)
...@@ -190,3 +209,4 @@ class client : ...@@ -190,3 +209,4 @@ class client :
if self.socket : if self.socket :
self._send(self._GMSH_STOP, 'Goodbye!') self._send(self._GMSH_STOP, 'Goodbye!')
self.socket.close() self.socket.close()
print 'Destructor called for %s' %(self.name)
#!/usr/bin/env python
#coding=utf-8
import OnelabClient
oc = OnelabClient.client()
A = oc.get_number('A')
B = oc.def_number('Group/B', 20)
print 'A= %f B = %f' %(A, B)
print('Action=%s' %(oc.get_string('python/Action')))
...@@ -7,30 +7,32 @@ oc = OnelabClient.client() ...@@ -7,30 +7,32 @@ oc = OnelabClient.client()
#name and default value are required #name and default value are required
A = oc.def_number('A', 10) A = oc.def_number('A', 10)
#other attributes are optionals #other attributes are optionals
B = oc.def_number('Group/B', 0, min = -10, max = 10, step = 1) #B = oc.def_number('Group/B', 0, min = -10, max = 10, step = 1)
C = oc.def_number('Group/C', 2, choices = [0, 1, 2, 3], attributes={'Highlight':'Pink'})
D = oc.def_number('Group/D', 2, labels = {0:'zero', 1:'un', 2:'deux', 3:'trois'}, attributes={'Highlight':'Blue'})
#utf-8 are allowed everywhere (should be prefixed by 'u' in python 2, not required in python 3)
#Omega = oc.get_string(u'Ω', u'∫(∂φ/∂α)³dx', help=u'ask someone@universe.org', choices = ['oui', 'non', u'peut-être'])
modelName = 'coin' modelName = 'coin'
oc.merge_file(modelName + '.geo') oc.merge_file(modelName + '.geo')
#print('Action=%s pour %s' %(oc.action,'python'))
print('Action=%s' %(oc.get_string('python/Action'))) print('Action=%s' %(oc.get_string('python/Action')))
oc.sub_client('gmsh', 'gmsh ' + modelName + '.geo -2') oc.sub_client('gmsh', 'gmsh ' + modelName + '.geo -2')
oc.merge_file(modelName + '.msh') oc.merge_file(modelName + '.msh')
oc.convert_olfile(modelName + '.txt') oc.sub_client('subclient', 'sub.py')
print 'script is waiting until subclient is done'
oc.wait_on_subclient('subclient');
oc.sub_client('python', 'python ' + modelName + '.py') C = oc.def_number('Group/C', 2, choices = [0, 1, 2, 3], attributes={'Highlight':'Pink'})
D = oc.def_number('Group/D', 2, labels = {0:'zero', 1:'un', 2:'deux', 3:'trois'}, attributes={'Highlight':'Blue'})
#utf-8 are allowed everywhere (should be prefixed by 'u' in python 2,
#not required in python 3)
#Omega = oc.get_string(u'Ω', u'∫(∂φ/∂α)³dx', help=u'ask someone@universe.org',
#choices = ['oui', 'non', u'peut-être'])
if oc.action != 'compute' : oc.convert_olfile(modelName + '.txt')
exit(0)
## insert here the client's script
if oc.action != 'compute' :
exit(0) exit(0)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment