git://git.onelab.eu
/
sfa.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
python3 - 2to3 + miscell obvious tweaks
[sfa.git]
/
sfa
/
client
/
multiclient.py
diff --git
a/sfa/client/multiclient.py
b/sfa/client/multiclient.py
index
75573ed
..
ad6f357
100644
(file)
--- a/
sfa/client/multiclient.py
+++ b/
sfa/client/multiclient.py
@@
-1,9
+1,12
@@
+
+
import threading
import traceback
import time
import threading
import traceback
import time
-from
Q
ueue import Queue
+from
q
ueue import Queue
from sfa.util.sfalogging import logger
from sfa.util.sfalogging import logger
+
def ThreadedMethod(callable, results, errors):
"""
A function decorator that returns a running thread. The thread
def ThreadedMethod(callable, results, errors):
"""
A function decorator that returns a running thread. The thread
@@
-11,20
+14,20
@@
def ThreadedMethod(callable, results, errors):
results queue
"""
def wrapper(args, kwds):
results queue
"""
def wrapper(args, kwds):
- class ThreadInstance(threading.Thread):
+ class ThreadInstance(threading.Thread):
+
def run(self):
try:
results.put(callable(*args, **kwds))
def run(self):
try:
results.put(callable(*args, **kwds))
- except Exception
,
e:
+ except Exception
as
e:
logger.log_exc('MultiClient: Error in thread: ')
errors.put(traceback.format_exc())
logger.log_exc('MultiClient: Error in thread: ')
errors.put(traceback.format_exc())
-
+
thread = ThreadInstance()
thread.start()
return thread
return wrapper
thread = ThreadInstance()
thread.start()
return thread
return wrapper
-
class MultiClient:
"""
class MultiClient:
"""
@@
-37,7
+40,7
@@
class MultiClient:
self.errors = Queue()
self.threads = []
self.errors = Queue()
self.threads = []
- def run
(self, method, *args, **kwds):
+ def run(self, method, *args, **kwds):
"""
Execute a callable in a separate thread.
"""
"""
Execute a callable in a separate thread.
"""
@@
-66,11
+69,11
@@
class MultiClient:
results = []
if not lenient:
errors = self.get_errors()
results = []
if not lenient:
errors = self.get_errors()
- if errors:
+ if errors:
raise Exception(errors[0])
while not self.results.empty():
raise Exception(errors[0])
while not self.results.empty():
- results.append(self.results.get())
+ results.append(self.results.get())
return results
def get_errors(self):
return results
def get_errors(self):
@@
-88,24
+91,25
@@
class MultiClient:
Get the value that should be returuned to the client. If there are errors then the
first error is returned. If there are no errors, then the first result is returned
"""
Get the value that should be returuned to the client. If there are errors then the
first error is returned. If there are no errors, then the first result is returned
"""
-
-
+
+
if __name__ == '__main__':
def f(name, n, sleep=1):
nums = []
if __name__ == '__main__':
def f(name, n, sleep=1):
nums = []
- for i in range(n, n
+
5):
- print
"%s: %s" % (name, i
)
+ for i in range(n, n
+
5):
+ print
("%s: %s" % (name, i)
)
nums.append(i)
time.sleep(sleep)
return nums
nums.append(i)
time.sleep(sleep)
return nums
+
def e(name, n, sleep=1):
nums = []
def e(name, n, sleep=1):
nums = []
- for i in
range(n, n+3
) + ['n', 'b']:
- print
"%s: 1 + %s:" % (name, i
)
+ for i in
list(range(n, n + 3)
) + ['n', 'b']:
+ print
("%s: 1 + %s:" % (name, i)
)
nums.append(i + 1)
time.sleep(sleep)
nums.append(i + 1)
time.sleep(sleep)
- return nums
+ return nums
threads = MultiClient()
threads.run(f, "Thread1", 10, 2)
threads = MultiClient()
threads.run(f, "Thread1", 10, 2)
@@
-114,7
+118,6
@@
if __name__ == '__main__':
#results = threads.get_results()
#errors = threads.get_errors()
#results = threads.get_results()
#errors = threads.get_errors()
- #print "Results:", results
- #print "Errors:", errors
+ #
print "Results:", results
+ #
print "Errors:", errors
results_xlenient = threads.get_results(lenient=False)
results_xlenient = threads.get_results(lenient=False)
-