preloading python modules with jedi?

227 views
Skip to first unread message

Jacques Kvam

unread,
Feb 27, 2016, 7:48:55 PM2/27/16
to ycm-users
Hi, I work with numpy a lot. I was trying to see if I could get the semantic completions to return faster by preloading numpy in jedi. I was looking around the jedihttp module and I am trying to add another endpoint for jedi, then call it after the server starts. Has anyone tried this before? Would this just be a dead end?

Thanks,
Jacques

Jacques Kvam

unread,
Feb 27, 2016, 8:16:06 PM2/27/16
to ycm-users
Got something crude working, it introduces about a second of lag when I start vim but it's acceptable for now.

First I added another route in jedihttp/handlers.py

@app.post( '/loadnumpy' )
def loadnumpy():
  logger.debug( 'received /loadnumpy request' )
  return jedi.preload_module('numpy')

Then in JediCompleter init I wait until the server is ready before I load numpy.
  
    self._StartServer()
    from time import sleep
    while not self.ServerIsReady():
        sleep(0.1)
    self._GetResponse('/loadnumpy')

Does the trick but I am hoping I can accomplish this in an asynchronous fashion. Any suggestions for that would be helpful.

Andrea Cedraro

unread,
Feb 28, 2016, 11:48:47 AM2/28/16
to ycm-users
Hi, I'm the author JediHTTP. I had planned this kind of feature but I didn't have enough time to sit down and experiment with it :(

Anyway what I had in mind was actually to add some kind of anyncronous handle to JediHTTP to just send a message and let JediHTTP do the work without waiting for the response. My original plan after getting that in place was that when YCM send the FileReadyToParse event to ycmd (which happen when you save the file or you don't move the cursor for an amount of seconds) to get the import statements of the file and preload the module so when you type a `.` you've already got those loaded. One thing that stopped me from doing that is that jedi is not thread safe so I don't know what would happen if we send a second request before the first finishes. What we could think though is to add a function for python in the ycm_extra_conf.py script to configure JediHTTP to preload some module. Actually some work is already needed to handle better the `sys.path` that jedi see to get completions, I just have to find the time :(

Jacques Kvam

unread,
Feb 29, 2016, 4:44:06 PM2/29/16
to ycm-users
Cool to hear that you've been thinking how to do this. Hope I'm not just taking up your time since I don't know the YCM code very well. I was just kind of hacking around for a couple hours before I got to that code snippet. I think I need to get a little more familiar with the codebase.

Both ideas sound promising. This may be a dumb question, but is it possible to make jedihttp stateful, in that it wouldn't send a second request until the first request finishes.
Reply all
Reply to author
Forward
0 new messages