Skip to content
Snippets Groups Projects
Commit 10b434ff authored by Danilo Piparo's avatar Danilo Piparo
Browse files

Make general and valid also for the non kernel case

parent 82ba1af0
No related branches found
No related tags found
No related merge requests found
# -*- coding:utf-8 -*- # -*- coding:utf-8 -*-
#----------------------------------------------------------------------------- #-----------------------------------------------------------------------------
# Copyright (c) 2015, ROOT Team. # Copyright (c) 2015, ROOT Team.
# Authors: Omar Zapata <Omar.Zapata@cern.ch> http://oproject.org # Authors: Danilo Piparo
# website: http://oproject.org/ROOT+Jupyter+Kernel (information only for ROOT kernel) # Omar Zapata <Omar.Zapata@cern.ch> http://oproject.org
# Distributed under the terms of the Modified LGPLv3 License. # Distributed under the terms of the Modified LGPLv3 License.
# #
# The full license is in the file COPYING.rst, distributed with this software. # The full license is in the file COPYING.rst, distributed with this software.
#----------------------------------------------------------------------------- #-----------------------------------------------------------------------------
from __future__ import print_function
from ctypes import CDLL, c_char_p from ctypes import CDLL, c_char_p
from threading import Thread from threading import Thread
from time import sleep as timeSleep
_lib = CDLL("libJupyROOT.so") _lib = CDLL("libJupyROOT.so")
...@@ -43,27 +46,40 @@ class IOHandler(object): ...@@ -43,27 +46,40 @@ class IOHandler(object):
def GetStderr(self): def GetStderr(self):
return _GetStream(_lib.JupyROOTExecutorHandler_GetStderr) return _GetStream(_lib.JupyROOTExecutorHandler_GetStderr)
def GetStreamsDicts(self):
out = self.GetStdout()
err = self.GetStderr()
outDict = {'name': 'stdout', 'text': out} if out != "" else None
errDict = {'name': 'stderr', 'text': err} if err != "" else None
return outDict,errDict
class Runner(object): class Runner(object):
def __init__(self, function): def __init__(self, function):
self.function = function self.function = function
self.thread = None
def Run(self, argument): def Run(self, argument):
return self.function(argument) return self.function(argument)
def AsyncRun(self, argument): def AsyncRun(self, argument):
self.thread = threading.Thread(target=self.Run, args =(argument,)) self.thread = Thread(target=self.Run, args =(argument,))
self.thread.start() self.thread.start()
def Wait(self):
if not self.thread: return
self.thread.join()
def HasFinished(self): def HasFinished(self):
finished = False if not self.thread: return True
if self.thread:
finished = not self.thread.is_alive() finished = not self.thread.is_alive()
if not finished: return False
self.thread.join()
self.thread = None
if finished: return True
self.thread.join()
self.thread = None
return finished
class JupyROOTDeclarer(Runner): class JupyROOTDeclarer(Runner):
def __init__(self): def __init__(self):
...@@ -73,3 +89,16 @@ class JupyROOTExecutor(Runner): ...@@ -73,3 +89,16 @@ class JupyROOTExecutor(Runner):
def __init__(self): def __init__(self):
super(JupyROOTExecutor, self).__init__(_lib.JupyROOTExecutor) super(JupyROOTExecutor, self).__init__(_lib.JupyROOTExecutor)
def RunAsyncAndPrint(executor, code, ioHandler, printFunction, silent = False, timeout = 0.1):
ioHandler.Clear()
ioHandler.InitCapture()
executor.AsyncRun(code)
while not executor.HasFinished():
ioHandler.Clear()
ioHandler.Poll()
if not silent:
printFunction(ioHandler)
if executor.HasFinished(): break
timeSleep(.1)
executor.Wait()
ioHandler.EndCapture()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment