On Fri, Dec 05, 2008 at 10:33:31PM +0100, Bram Moolenaar wrote:
> 
> 
> Marc Weber wrote:
> 
> > > Please explain what this would do what vim.command() isn't doing.
> > > I mean the one explained below ":he python-vim".
> > 
> > You can start multiple threads in python. The help didn't say tat
> > vim.command() is thread safe. However it would be nice to replace this
> > executeVimCommand hackery with a more native, non blocking thread safe
> > command.  Then you could start writing nice interfaces for debuggers
> > more easily.
> 
> I do think that vim.command() is thread safe.  Not 100% sure, I didn't
> look at the detailed documentation for Python native interface.  The
> code contains:
> 
>     Py_BEGIN_ALLOW_THREADS
>     Python_Lock_Vim();
> 
>     do_cmdline_cmd((char_u *)cmd);
>     update_screen(VALID);
> 
>     Python_Release_Vim();
>     Py_END_ALLOW_THREADS
> 
> Looks like a lock is used to guarantee only one command is done at a
> time.  And that's required, since the Vim core is defenitely not
> thread-safe.

Hi Bram,

Could you try running the testcase at the end of the file?
it starts 200 threads each running a loop
200 times running vim.cmd.
It segfaults here.

Have you actually had a look at the code. The implementation is empty
(stripped :)
static void Python_Lock_Vim(void) { }
static void Python_Release_Vim(void) { }
:-)
no actions (current SVN HEAD)
I've also searched for Py_BEGIN_ALLOW_THREADS. All occurences are within
if_pyhton.c

Even if the a lock had been aquired there it wouldn't have been
enough unless vim does so itself:
Imagine this: vim is running the vimscript garbage collector. Then a
pyhton thread runs vim.cmd().. It will acquire its lock.. and both, the
gc and the the python thread will start reading / writing the same
memory! So this requires kind of global lock anyway. But you know the
code much better than me. I guess you can just tell me wether such a
global lock does exist?

It could be fixed easily.. Just acquire the lock when any action is
triggerd. Sources which can trigger actions: Keyboard/ mouse input and
script threads such as the python one. Don't know about (perl, ruby,
...)

Sincerly
Marc Weber


" ==== test case segfaulting here ======================
" thread safe testcase
fun! Test()
py << EOF

from subprocess import Popen, PIPE
import threading
import os
import vim

print "python start"

from time import sleep

class MyThread ( threading.Thread ):
  def __init__(self, start, command):
    vim.command(start)
    threading.Thread.__init__(self)
    self.command=command

  def run ( self ):
    sleep(3)
    for i in range(1,200):
      vim.command(self.command)

vim.command("let g:started=0")

for i in range(1,200):
  thread=MyThread("let g:start=1 | echo \"starting\"", "let g:done=1|
echo \"succ\"")
  thread.start()
  vim.command("let g:started = g:started+1")

print "python thread started"
EOF
endfun

call Test()

--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_dev" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Raspunde prin e-mail lui