1

I am using a python library I wrote to interact with a custom USB device. The library needs to send and receive data. I also need to interactively call methods. At the moment I utilize two shells, one receiving only and the other sending only. The latter is in the (i)python REPL. It works but it is clumsy, so I want to consolidate the two things in a single shell, which will have the advantage to have access to data structures from both sides in one context. That works fine. The problem is in the UI.

In fact, the receiving part needs to asynchronously print some information. So I wrote something like the following:

#!/usr/bin/python

import threading
import time
import blessings

TOTAL=5

def print_time( thread_id, delay):
   count = 0
   t=blessings.Terminal()
   while count < TOTAL:
      time.sleep(delay)
      count += 1
      stuff = "Thread " + str(thread_id) + " " + str(time.ctime(time.time())) + " -- " + str(TOTAL - count) + " to go"
      with t.location(t.width - len(stuff) - 1, thread_id):
        print (stuff, end=None )
      print("", end="")  # just return the cursor

try:
   t1 = threading.Thread( target = print_time, args = (1, 2, ) )
   t1.start()
   print ("Thread started")
except:
   print ("Error: unable to start thread")

That is my __init__.py file for the module. It somewhat works, but it has two problems:

  • While the thread is running, you cannot exit the REPL neither with CTRL-D nor with sys.exit() (that is the reason I am using TOTAL=5 above, so your life is easier if you try this code). This is a problem since my actual thread needs to be an infinite loop. I guess one solution could be to exit via a custom call which will cause a break into that infinite loop, but is there anything better?

  • The cursor does not return correctly to its earlier position

    • if I remove the end="" in the line with the comment # just return the cursor, it sort of works, but obviously print an unwanted newline in the place the cursor was (which messes us other input and/or output which might be happening there, in addition to add that unwanted newline)
    • if I leave the end="" it does not return the cursor, not even if I add something to print, e.g. print(".", end="") -- the dots . are printed at the right place, but the blinking cursor and the input is printed at the top

I know these are two unrelated problem and I could have asked two separate questions, but I need an answer to both, or otherwise it's a moot point. Or alternatively, I am open to other solutions. I thought of a separate GTK window, and that might work, but it's a subpar solution, since I really would like this to work in CLI only (to keep it possible in a ssh-without-X-tunneling setup).

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Davide
  • 17,098
  • 11
  • 52
  • 68

1 Answers1

1

Using blessed instead of blessing does not have the problem with the cursor not returning to the previous position, even without anything outside of the with context.

Making the thread a daemon solves the other problem.

Davide
  • 17,098
  • 11
  • 52
  • 68