Martin Conte Mac Donell | 3 Oct 2008 06:33
Picon
Gravatar

Urwid & threads.

Hello,

I'm developing some applications with urwid but i don't get how should
i get input in a different thread. I tried to start a new thread and
listen to get_input() having time.sleep in a main thread. When i raise
a resize event my script quits. (I don't know what is GIL doing here
but sleep just ends).

I'm pretty sure there is another better way to do this. What is this way?

Thank you,
M
Ian Ward | 7 Oct 2008 04:48
Favicon
Gravatar

Re: Urwid & threads.

Martin Conte Mac Donell wrote:
> Hello,
> 
> I'm developing some applications with urwid but i don't get how should
> i get input in a different thread. I tried to start a new thread and
> listen to get_input() having time.sleep in a main thread. When i raise
> a resize event my script quits. (I don't know what is GIL doing here
> but sleep just ends).
> 
> I'm pretty sure there is another better way to do this. What is this way?

I don't use threads in my applications, but I might be able to help. 
Would you post a simple program that demonstrates the problem?

Ian
Sharad Ganapathy | 17 Oct 2008 13:26
Picon

Error on running the first tutorial program

Hi,

I want to develop a curses based app. ( new to curses also) and found 
urwid.

I have installed the latest urwid release and tried my hand running the 
first tutorial .  I am getting the following error .

dropalready-lm:python sharadg$ ./urwid_ex.py
Traceback (most recent call last):
   File "./urwid_ex.py", line 8, in <module>
     rows,cols = ui.get_cols_rows()
   File "build/bdist.macosx-10.5-i386/egg/urwid/curses_display.py", line 
497, in get_cols_rows
AttributeError: 'NoneType' object has no attribute 'getmaxyx'

Code :

#!/usr/bin/python

import urwid.curses_display
import urwid
import sys

ui = urwid.curses_display.Screen()
cols,rows = ui.get_cols_rows()
print "Row is %d and Cols is %d" %(rows,cols)

def run():
	canvas = urwid.TextCanvas("[Hello World]")
(Continue reading)

Ian Ward | 17 Oct 2008 21:20
Favicon
Gravatar

Re: Error on running the first tutorial program

Sharad Ganapathy wrote:
> I have installed the latest urwid release and tried my hand running the 
> first tutorial .  I am getting the following error .
> 
> dropalready-lm:python sharadg$ ./urwid_ex.py
> Traceback (most recent call last):
>    File "./urwid_ex.py", line 8, in <module>
>      rows,cols = ui.get_cols_rows()
>    File "build/bdist.macosx-10.5-i386/egg/urwid/curses_display.py", line 
> 497, in get_cols_rows
> AttributeError: 'NoneType' object has no attribute 'getmaxyx'
> 
> 
> Code :
> 
> #!/usr/bin/python
> 
> import urwid.curses_display
> import urwid
> import sys
> 
> ui = urwid.curses_display.Screen()
> cols,rows = ui.get_cols_rows()
> print "Row is %d and Cols is %d" %(rows,cols)
> 
> def run():
> 	canvas = urwid.TextCanvas("[Hello World]")
> 	ui.draw_screen((20,1),canvas)
> 	
> 	while not ui.get_input():
(Continue reading)

Sharad Ganapathy | 18 Oct 2008 07:39
Picon

Re: Error on running the first tutorial program

Ian Ward wrote:

> 
> Hi Sharad,
> 
> What version of Urwid are you using?
> 
> What happens when you just try to "import curses" from the python 
> command line?
> 
> Ian

Ian ,

 From the python command line :

 >>> print urwid.__version__
0.9.8.3
 >>> import curses
 >>> print curses.version
2.2

Sharad
Martin Conte Mac Donell | 18 Oct 2008 20:06
Picon
Gravatar

Re: Urwid & threads.

On Mon, Oct 6, 2008 at 11:48 PM, Ian Ward <ian <at> excess.org> wrote:
>
> I don't use threads in my applications, but I might be able to help.
> Would you post a simple program that demonstrates the problem?
>
> Ian

Hello Ian

Small example:

import time
import threading
import urwid.curses_display
import urwid

class Screen(threading.Thread):
    def run(self):
        self.ui = urwid.curses_display.Screen()
        self.ui.run_wrapper(self._run)

    def _run(self):
        size = self.ui.get_cols_rows()
        txt = urwid.Text("TEST", align="center")
        fill = urwid.Filler(txt)
        canvas = fill.render(size)
        self.ui.draw_screen(size, canvas)

        while True:
            input_list = self.ui.get_input()
(Continue reading)

Martin Conte Mac Donell | 18 Oct 2008 20:18
Picon
Gravatar

Re: Urwid & threads.

On Sat, Oct 18, 2008 at 3:06 PM, Martin Conte Mac Donell
<reflejo <at> gmail.com> wrote:
> On Mon, Oct 6, 2008 at 11:48 PM, Ian Ward <ian <at> excess.org> wrote:
>>
>> I don't use threads in my applications, but I might be able to help.
>> Would you post a simple program that demonstrates the problem?
>>
>> Ian

BTW

>>> curses.version
'2.2'
>>> urwid.__version__
'0.9.8.1'

Regards
M
Sascha Silbe | 18 Oct 2008 23:59

Re: Urwid & threads.

On Sat, Oct 18, 2008 at 03:06:45PM -0300, Martin Conte Mac Donell wrote:

> t.start()
> time.sleep(2000000)
> raise Exception("Why?")
>
> Exception is raised when i resize my xterm window. For some reason
> this action breaks time.sleep.
That's the way signals + threads work on a POSIX system. Signals from 
other processes are delivered to a single, arbitrarily chosen thread 
(see POSIX 2.4.1, "Signal Generation and Delivery" [2]). In your case 
the main thread was chosen (but you cannot rely on that).
In the thread handling the signal the currently executed OS function 
will usually abort and return with EINTR.
See also Entry #10 of the comp.programming.threads FAQ [1].

[1] http://www.faqs.org/faqs/threads-faq/part1/
[2] 
http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html

CU Sascha

--

-- 
http://sascha.silbe.org/
http://www.infra-silbe.de/
_______________________________________________
Urwid mailing list
Urwid <at> lists.excess.org
(Continue reading)

Ian Ward | 20 Oct 2008 18:53
Favicon
Gravatar

Re: Urwid & threads.

Sascha Silbe wrote:
> On Sat, Oct 18, 2008 at 03:06:45PM -0300, Martin Conte Mac Donell wrote:
> 
>> t.start()
>> time.sleep(2000000)
>> raise Exception("Why?")
>>
>> Exception is raised when i resize my xterm window. For some reason
>> this action breaks time.sleep.
> That's the way signals + threads work on a POSIX system. Signals from 
> other processes are delivered to a single, arbitrarily chosen thread 
> (see POSIX 2.4.1, "Signal Generation and Delivery" [2]). In your case 
> the main thread was chosen (but you cannot rely on that).
> In the thread handling the signal the currently executed OS function 
> will usually abort and return with EINTR.
> See also Entry #10 of the comp.programming.threads FAQ [1].
> 
> 
> [1] http://www.faqs.org/faqs/threads-faq/part1/
> [2] 
> http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html

So, one solution would be to use processes instead of threads (generally 
good advice in any case)  But I wonder if disabling the signal that you 
are being sent would also work..  I'll try it when I have a few minutes.

Ian
Martin Conte Mac Donell | 20 Oct 2008 19:32
Picon
Gravatar

Re: Urwid & threads.

On Mon, Oct 20, 2008 at 2:53 PM, Ian Ward <ian <at> excess.org> wrote:
>
> So, one solution would be to use processes instead of threads (generally
> good advice in any case)  But I wonder if disabling the signal that you
> are being sent would also work..  I'll try it when I have a few minutes.

The problem using processing instead of threads is that inter-process
communication isn't that trivial. Reading the urwid source code i also
realize  that the original approach was wrong.

I should catch input from the main thread and keep the other logic in
other thread. With this approach, of course it works.

Thank you for your time.
M.

Gmane