This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Wrap rl_forced_update_display() and lock readline module while active
Type: enhancement Stage: patch review
Components: Extension Modules Versions: Python 3.6
process
Status: open Resolution:
Dependencies: 23067 Superseder:
Assigned To: Nosy List: Barney Stratford, martin.panter, twouters
Priority: normal Keywords:

Created on 2015-07-26 10:57 by Barney Stratford, last changed 2022-04-11 14:58 by admin.

Files
File name Uploaded Description Edit
patch.txt Barney Stratford, 2015-07-26 10:57 review
patch.txt Barney Stratford, 2015-08-02 16:37 review
Messages (4)
msg247424 - (view) Author: Barney Stratford (Barney Stratford) * Date: 2015-07-26 10:57
I have a use case where a daemon thread needs to write periodic updates to sys.stdout. I'd prefer it not to print in the middle of a command being typed. In order to achieve this, I have added to the readline module. There is now a function to determine whether we are reading a line, and one to call rl_forced_update_display. These functions enable me to say (in the daemon thread):

def describe (indicator):
    if readline.reading_line ():
        print ()
        print (indicator.description)
        readline.forced_update_display ()
    else:
        print (indicator.description)


I have read PEP7, and have made patchtest on my patch.
msg247849 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2015-08-02 01:30
Suggest you integrate your rl_forced_update_display() wrapper and documentation with the existing patches in Issue 23067.

Regarding the reading_line() flag, your use case seems a bit racy to me. Can’t the other thread start reading a line after reading_line() returns false and before your print() statement happens?
msg247881 - (view) Author: Barney Stratford (Barney Stratford) * Date: 2015-08-02 16:37
You're absolutely right. I had elided the details of the locking from my original use case, but now I look at it again, I see that even what I did have wasn't enough.

I've made a new patch that takes a lock properly. A non-blocking acquisition of the lock will serve the same purpose as looking to see if there is a line being read, and will also prevent the interpreter from starting to read a new line while the text is being printed.

My use case now looks like this.

reading_lock = threading.Lock ()
readline.set_lock (reading_lock)

def describe (indicator):
	if reading_lock.acquire (False):
		print (indicator.description)
		reading_lock.release ()
	else:
		print ()
		print (indicator.description)
		readline.forced_update_display ()
msg268965 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2016-06-21 04:23
I wonder why you can’t add the locking around input() calls at a higher level, rather than needing to change the readline module itself.
History
Date User Action Args
2022-04-11 14:58:19adminsetgithub: 68915
2016-06-21 04:23:10martin.pantersettitle: Expand readline module -> Wrap rl_forced_update_display() and lock readline module while active
messages: + msg268965
stage: patch review
2015-08-02 16:37:23Barney Stratfordsetfiles: + patch.txt

messages: + msg247881
2015-08-02 01:30:30martin.pantersetnosy: + martin.panter
dependencies: + Export readline forced_update_display
messages: + msg247849
2015-07-27 17:43:43r.david.murraysetversions: - Python 3.5
2015-07-26 17:06:39ned.deilysetnosy: + twouters
2015-07-26 12:46:08Barney Stratfordsetcomponents: + Extension Modules, - Library (Lib)
2015-07-26 10:57:51Barney Stratfordcreate