Skip to content

Wrap rl_forced_update_display() and lock readline module while active #68915

Open

Description

@BarneyStratford
BPO 24727
Nosy @Yhg1s, @vadmium, @BarneyStratford
Dependencies
  • bpo-23067: Export readline forced_update_display
  • Files
  • patch.txt
  • patch.txt
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = None
    created_at = <Date 2015-07-26.10:57:51.248>
    labels = ['extension-modules', 'type-feature']
    title = 'Wrap rl_forced_update_display() and lock readline module while active'
    updated_at = <Date 2016-06-21.04:23:10.368>
    user = 'https://github.com/BarneyStratford'

    bugs.python.org fields:

    activity = <Date 2016-06-21.04:23:10.368>
    actor = 'martin.panter'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['Extension Modules']
    creation = <Date 2015-07-26.10:57:51.248>
    creator = 'Barney Stratford'
    dependencies = ['23067']
    files = ['40029', '40104']
    hgrepos = []
    issue_num = 24727
    keywords = []
    message_count = 4.0
    messages = ['247424', '247849', '247881', '268965']
    nosy_count = 3.0
    nosy_names = ['twouters', 'martin.panter', 'Barney Stratford']
    pr_nums = []
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue24727'
    versions = ['Python 3.6']

    Activity

    1. BarneyStratford commented on Jul 26, 2015

      BarneyStratfordmannequin
      MannequinAuthor

      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 PEP-7, and have made patchtest on my patch.

    2. added
      stdlibStandard Library Python modules in the Lib/ directory
      type-featureA feature request or enhancement
      and removed
      stdlibStandard Library Python modules in the Lib/ directory
      on Jul 26, 2015
    3. vadmium commented on Aug 2, 2015

      @vadmium
      Member

      Suggest you integrate your rl_forced_update_display() wrapper and documentation with the existing patches in bpo-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?

    4. BarneyStratford commented on Aug 2, 2015

      BarneyStratfordmannequin
      MannequinAuthor

      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 ()
    5. vadmium commented on Jun 21, 2016

      @vadmium
      Member

      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.

    6. changed the title [-]Expand readline module[/-] [+]Wrap rl_forced_update_display() and lock readline module while active[/+] on Jun 21, 2016
    7. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      extension-modulesC modules in the Modules dirtype-featureA feature request or enhancement

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions