Repository: tensorlayer/TensorLayer

Issue:
Issues with ops.exit_tf function
So far I've 2 issues with this function :
- ops.exit_tf() returns an error because a session is expected as argument.
- ops.exit_tf(sess) returns "NameError: name 'exit' is not defined" when launched from an IPython console - in my case it's Spyder's internal IPython console (tested both on Mac and Windows)

Both issue are easy to solve :
- add `if sess != None:` before `sess.close()`
- use `from sys import exit as _exit` to avoid whatever is the interference that's throwing the error

Before I propose a pull request, I just wanted to check that I'm not misusing the function : I tried to use the function without an argument to easly exit tensorboard (which is a nice feature) and I'm launching it from IPyton instead of the terminal because it's seems like a usefull function to be able to include in a script (and i run my scripts from Spyder)


Discussion:
Hi, in my view the changes are all correct, thank you.
OK, will do a PR tomorrow.

Side note : I went quickly through your docs and you mention in the Testing section of Development that you want a test case for each bug fix. I'm guessing this is no longer required (since i can't find any test in the code)
yes, it doesn't require.



Resolving patch:
diff --git a/tensorlayer/ops.py b/tensorlayer/ops.py
--- a/tensorlayer/ops.py
+++ b/tensorlayer/ops.py
@@ -5,36 +5,73 @@
 
 
 import tensorflow as tf
+import tensorlayer as tl
 import os
+import subprocess
 import sys
 from sys import platform as _platform
+from sys import exit as _exit
 
 
-def exit_tf(sess=None):
-    """Close tensorboard and nvidia-process if available
+def exit_tf(sess=None, port=6006):
+    """Close tensorflow session, tensorboard and nvidia-process if available
 
     Parameters
     ----------
     sess : a session instance of TensorFlow
         TensorFlow session
+    tb_port : an integer
+        TensorBoard port you want to close, 6006 is tensorboard default
     """
     text = "[tl] Close tensorboard and nvidia-process if available"
-    sess.close()
+    text2 = "[tl] Close tensorboard and nvidia-process not yet supported by this function (tl.ops.exit_tf) on "
+    if sess != None:
+        sess.close()
     # import time
     # time.sleep(2)
     if _platform == "linux" or _platform == "linux2":
         print('linux: %s' % text)
         os.system('nvidia-smi')
-        os.system('fuser 6006/tcp -k')  # kill tensorboard 6006
+        os.system('fuser '+ port +'/tcp -k')  # kill tensorboard 6006
         os.system("nvidia-smi | grep python |awk '{print $3}'|xargs kill") # kill all nvidia-smi python process
+        _exit()
     elif _platform == "darwin":
         print('OS X: %s' % text)
-        os.system("lsof -i tcp:6006 | grep -v PID | awk '{print $2}' | xargs kill") # kill tensorboard 6006
+        subprocess.Popen("lsof -i tcp:"+ str(port) +"  | grep -v PID | awk '{print $2}' | xargs kill", shell=True) # kill tensorboard
     elif _platform == "win32":
-        print('Windows: %s' % text)
+        print(text2 + "Windows")
+        # TODO
     else:
-        print(_platform)
-    exit()
+        print(text2 + _platform)
+    
+def open_tb(logdir='/tmp/tensorflow', port=6006):
+    """Open tensorboard
+    
+    Parameters
+    ----------
+    logdir : a string
+        Directory where your tensorboard logs are saved
+    port : an integer
+        TensorBoard port you want to open, 6006 is tensorboard default
+    """
+
+    text = "[tl] Open tensorboard, go to localhost:" + str(port) + " to access"
+    text2 = " not yet supported by this function (tl.ops.open_tb)"
+    
+    if not tl.files.exists_or_mkdir(logdir, verbose=False):
+        print("[tl] Log reportory was created at %s" % logdir)
+    
+    if _platform == "linux" or _platform == "linux2":
+        print('linux %s' % text2)
+        # TODO
+    elif _platform == "darwin":
+        print('OS X: %s' % text)
+        subprocess.Popen(sys.prefix + " | python -m tensorflow.tensorboard --logdir=" + logdir + " --port=" + str(port), shell=True) # open tensorboard in localhost:6006/ or whatever port you chose
+    elif _platform == "win32":
+        print('Windows%s' % text2)
+        # TODO
+    else:
+        print(_platform + text2)
 
 def clear_all(printable=True):
     """Clears all the placeholder variables of keep prob,


Test patch: