Author: Date: Subject:
Stef Mientki
2008-07-18 13:34:56
[wxpython-users] Is this the correct way to use threads (was Babel
Fish translation) ?
hello,
For translation of selected text in a texteditor (Scintilla),
I use Babel Fish.
As it takes a while, before Babel Fish responds,
a normal call to Babel Fish, will freeze the wxPython application.
With the help of other Python users,
I implemented a thread construction,
which indeed seems to work as expected.
Now my question are:
Is this the correct way to use threads in wxPython ?
Should I kill or destroy the thread ?
Here is the essential code:
If the user moves to a piece of text that must be translated,
the event handler will start a thread
self.Thread = threading.Thread ( target = self.Read_Bablefish,
args = ( [ text ] ) )
self.Thread.start()
The thread starts this procedure:
# *************************************************************
# *************************************************************
def Read_Bablefish ( self, text ) :
BABLEFISH_URL = ' http://babelfish.altavista.com/tr'
url = BABLEFISH_URL + '?' +\
urlencode ( { 'trtext' : text,
'lp' : 'en_' + self.Language_Current.lower
() } )
page = lxml.html.parse(url)
Babel_Result = []
for div in page.iter ( 'div' ) :
style = div.get ( 'style' )
if ( style != None ) and ( 'padding:0.6em;' in style ) :
Babel_Result.append(
lxml.html.tostring ( div, method = "text" ) )
# if a result is found, it's placed directly into a textwindow
if Babel_Result :
self.Editor_Babel.SetLabel ( Babel_Result[0] )
else :
self.Editor_Babel.SetLabel ( 'Babel Fish translation Failed' )
thanks,
Stef Mientki
_______________________________________________
wxpython-users mailing list
[email protected]
http://lists.wxwidgets.org/mailman/listinfo/wxpython-users
Author: Date: Subject:
Tim van der Leeuw
2008-07-18 13:42:47
Re: [wxpython-users] Is this the correct way to use threads (was
Babel Fish translation) ?
Hi Stef,
On Fri, Jul 18, 2008 at 1:34 PM, Stef Mientki <[email protected]> wrote:
> hello,
>
> For translation of selected text in a texteditor (Scintilla),
>
[...]
>
> Now my question are:
> Is this the correct way to use threads in wxPython ?
> Should I kill or destroy the thread ?
>
No, you don't have to do that.
>
> Here is the essential code:
>
> If the user moves to a piece of text that must be translated,
> the event handler will start a thread
>
[...]
>
> # if a result is found, it's placed directly into a textwindow
> if Babel_Result :
> self.Editor_Babel.SetLabel ( Babel_Result[0] )
> else :
> self.Editor_Babel.SetLabel ( 'Babel Fish translation Failed' )
>
>
Perhaps Scintilla is thread-safe, but the rule is that you should not make
any wx-calls from a thread. The only things which are safe to call from a
thread are:
- wx.CallLater,
- wx.CallAfter,
- wx.Thread_IsMain.
This is how you use wx.CallLater:
wx.CallLater(self.Editor_Babel.SetLabel, Babel_Result[0] )
As you can see, it's a small change to make :-)
Regards,
--Tim
<div dir="ltr">Hi Stef,<br><br><div class="gmail_quote">On Fri, Jul 18, 2008 at 1:34 PM, Stef Mientki <<a href="mailto:s.mientki@ru.nl">s.mientki@ru.nl</a>> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
hello,<br>
<br>
For translation of selected text in a texteditor (Scintilla),<br>
</blockquote><div><br>[...] <br></div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><br>
Now my question are:<br>
Is this the correct way to use threads in wxPython ?<br>
Should I kill or destroy the thread ?<br>
</blockquote><div><br>No, you don't have to do that.<br> </div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><br>
Here is the essential code:<br>
<br>
If the user moves to a piece of text that must be translated,<br>
the event handler will start a thread<br>
</blockquote><div><br>[...] <br></div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><br> # if a result is found, it's placed directly into a textwindow<br>
if Babel_Result :<br>
self.Editor_Babel.SetLabel ( Babel_Result[0] )<br>
else :<br>
self.Editor_Babel.SetLabel ( 'Babel Fish translation Failed' )<br>
<br>
</blockquote><div><br><br>Perhaps Scintilla is thread-safe, but the rule is that you should not make any wx-calls from a thread. The only things which are safe to call from a thread are:<br><ul><li> wx.CallLater,</li><li>
wx.CallAfter, <br></li><li>wx.Thread_IsMain.</li></ul><br>This is how you use wx.CallLater:<br><br>wx.CallLater(self.Editor_Babel.SetLabel, Babel_Result[0] )<br> <br><br>As you can see, it's a small change to make :-)<br>
<br><br>Regards,<br><br>--Tim<br><br></div></div><br></div>
_______________________________________________
wxpython-users mailing list
[email protected]
http://lists.wxwidgets.org/mailman/listinfo/wxpython-users
Author: Date: Subject:
Stef Mientki
2008-07-18 17:42:19
Re: [wxpython-users] Is this the correct way to use threads (was
Babel Fish translation) ?
thanks Tim,
[..]
>
>
> Perhaps Scintilla is thread-safe, but the rule is that you should not
> make any wx-calls from a thread. The only things which are safe to
> call from a thread are:
>
> * wx.CallLater,
> * wx.CallAfter,
> * wx.Thread_IsMain.
>
>
> This is how you use wx.CallLater:
>
> wx.CallLater(self.Editor_Babel.SetLabel, Babel_Result[0] )
>
>
> As you can see, it's a small change to make :-)
>
Yes, it's a small change indeed,
but it has enormous consequences ;-)
Exception in thread Thread-1:
Traceback (most recent call last):
File "P:\Python\Lib\threading.py", line 486, in __bootstrap_inner
self.run()
File "P:\Python\Lib\threading.py", line 446, in run
self.__target(*self.__args, **self.__kwargs)
File "D:\Data_Python\P24_support\multi_language.py", line 556, in
Read_Bablefish
wx.CallLater ( self.Editor_Babel.SetLabel, Babel_Result[0] )
File "P:\Python\lib\site-packages\wx-2.8-msw-unicode\wx\_core.py",
line 14404, in __init__
self.Start()
File "P:\Python\lib\site-packages\wx-2.8-msw-unicode\wx\_core.py",
line 14421, in Start
self.timer.Start(self.millis, wx.TIMER_ONE_SHOT)
File "P:\Python\lib\site-packages\wx-2.8-msw-unicode\wx\_misc.py",
line 1298, in Start
return _misc_.Timer_Start(*args, **kwargs)
TypeError: in method 'Timer_Start', expected argument 2 of type 'int'
cheers,
Stef
_______________________________________________
wxpython-users mailing list
[email protected]
http://lists.wxwidgets.org/mailman/listinfo/wxpython-users
Author: Date: Subject:
Stef Mientki
2008-07-18 17:52:06
Re: [wxpython-users] Is this the correct way to use threads (was
Babel Fish translation) ?
now I already found out, I have to specify a time,
but that doesn't make it better :-(
wx.CallLater ( 100, self.Editor_Babel.SetLabel, Babel_Result[0] )
Exception in thread Thread-1:
Traceback (most recent call last):
File "P:\Python\Lib\threading.py", line 486, in __bootstrap_inner
self.run()
File "P:\Python\Lib\threading.py", line 446, in run
self.__target(*self.__args, **self.__kwargs)
File "D:\Data_Python\P24_support\multi_language.py", line 556, in
Read_Bablefish
wx.CallLater ( 100, self.Editor_Babel.SetLabel, Babel_Result[0] )
File "P:\Python\lib\site-packages\wx-2.8-msw-unicode\wx\_core.py",
line 14404, in __init__
self.Start()
File "P:\Python\lib\site-packages\wx-2.8-msw-unicode\wx\_core.py",
line 14421, in Start
self.timer.Start(self.millis, wx.TIMER_ONE_SHOT)
File "P:\Python\lib\site-packages\wx-2.8-msw-unicode\wx\_misc.py",
line 1298, in Start
return _misc_.Timer_Start(*args, **kwargs)
PyAssertionError: C++ assertion "wxThread::IsMain()" failed at
..\..\src\common\timercmn.cpp(66) in wxTimerBase::Start(): timer can
only be started from the main thread
cheers,
Stef
_______________________________________________
wxpython-users mailing list
[email protected]
http://lists.wxwidgets.org/mailman/listinfo/wxpython-users
|