file write collision consideration

2009-01-20 Thread RGK
I have a thread that is off reading things some of which will get 
written into a file while another UI thread manages input from a user.


The reader-thread and the UI-thread will both want to write stuff to the 
same output file. What first comes to mind is that there may be write 
collisions, ie both trying to write at the same time.


Should I do something like:


if busyFlag:
  while busyFlag:
 sleep(10)
else:
  busyFlag = True
  {write stuff to file}
  busyFlag = False


in both threads?   Is there some other approach that would be more 
appropriate?


Thanks in advance for your advice
- Ross.
--
http://mail.python.org/mailman/listinfo/python-list


Re: file write collision consideration

2009-01-20 Thread RGK
Thanks for the suggestions - sounds like a couple good options, I 
apprecieate it.


Ross.

MRAB wrote:

RGK wrote:
I have a thread that is off reading things some of which will get 
written into a file while another UI thread manages input from a user.


The reader-thread and the UI-thread will both want to write stuff to 
the same output file. What first comes to mind is that there may be 
write collisions, ie both trying to write at the same time.


Should I do something like:


if busyFlag:
  while busyFlag:
 sleep(10)
else:
  busyFlag = True
  {write stuff to file}
  busyFlag = False


in both threads?   Is there some other approach that would be more 
appropriate?


Thanks in advance for your advice

If you're using the threading module, I suggest you look at the 
threading.RLock class:


my_lock = threading.RLock()
...
with my_lock:
{write stuff to file}

or, if you're using an older version of Python:

my_lock = threading.RLock()
...
my_lock.acquire()
{write stuff to file}
my_lock.release()

--
http://mail.python.org/mailman/listinfo/python-list


Profiling Python Apps on Mac?

2009-01-28 Thread RGK
I'm writing a python app on a Mac (in Eclipse + PyDev w/ Python2.5 & 
wxPython under OSX 10.4)


As I make program architecture decisions, it would be nice to be able to 
profile the choices.  Should I add that extra thread?  Is this big-assed 
xml object I just created horribly bloated or kind of ordinary.


Is there anything out there I should look into to if I want to see how 
those things are affecting my app?   The closest I have is the widget 
iStat, but it's a very static low resolution view of what's really going on.


Thx in advance.
Ross.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Profiling Python Apps on Mac?

2009-01-28 Thread RGK

Philip Semanchuk wrote:


Is there any reason that this wouldn't work?

http://docs.python.org/library/hotshot.html


It suggests that it doesn't work well with threads, but as I didn't know 
about any options, it's a step forward.  Thanks for the pointer.  :)


R.
--
http://mail.python.org/mailman/listinfo/python-list


coding style - try, except

2009-02-25 Thread RGK


I'm still learning, so eager to see if there is some community wisdom 
about use of the try/except structures in this situation.


I find myself with some potentially risky stuff and wrap it in a 
try/except structure with good functional results, though my code leaves 
me a bit uneasy. Maybe it's just esoteric, but your input is appreciated.


Consider

  try:
do something 1
do something 2
do something 3
do something 4
...
do something 25

  except:
print "Oops something didn't work"


The risky things are just 1 & 2, and the others are not of concern, but 
are dependent on 1 & 2.  The alternative is to do:


  wentOkay = True
  try:
do something 1
do something 2

  except:
print "Oops something didn't work"
wentOkay = False

  if wentOkay:
do something 3
do something 4
 ...
do something 25


Which seems a bit verbose, but likely the better approach.  Is there 
some other option I should be considering?


Any input appreciated :)

Ross.

--
http://mail.python.org/mailman/listinfo/python-list


Re: coding style - try, except

2009-02-25 Thread RGK


I'm glad I asked :)

Thanks all who posted for your replies, the else-statement is a nice 
option.


Python again comes through to deal with those pesky feelings that 
something could be better :)


Ross.



Chris Rebert wrote:


Yes. try-except-*else*.

try:
do_something_1()
do_something_2()
except:
print "Houston, we have a problem"
else: #runs only if no exception was thrown
do_something_3()
do_something_4()
et_cetera()

Cheers,
Chris


--
http://mail.python.org/mailman/listinfo/python-list


Writing to Console on mac OS X

2009-03-31 Thread RGK
I'm on mac os x 10.4.11 running python 2.5.2, and Django 1.0, but this 
is a python question.


When doing django/mod_python stuff, I can write to the Apache error_log 
file with


sys.stderr.write("SOMETHING I WANT TO KNOW")

which had me wondering if there's not a means for a misc. python program 
to write to the Mac OS X console?   That would be much nicer than having 
to open up the error log and inspect stuff, as then I could see debug 
info stream past on a console window.


(This is console, as in the "console" run from /Applications/Utilities, 
not the bash "Terminal")


Any help or suggestions appreciated. Thx.

Ross.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Writing to Console on mac OS X

2009-03-31 Thread RGK


Thanks for the pointer Irmen. That works fine.

Also my unfamiliarity with the console app is showing - I just learned 
that there is a navigation pane activated by the 'logs' icon that allows 
me to see various system logs, including the Apache ones :p


You're right, I've heard a bit about the Python logging module, but 
never looked into it. This is a good reason to take a look.


Thanks again & Regards,
Ross.

Irmen de Jong wrote:

RGK wrote:
I'm on mac os x 10.4.11 running python 2.5.2, and Django 1.0, but this 
is a python question.


When doing django/mod_python stuff, I can write to the Apache 
error_log file with


sys.stderr.write("SOMETHING I WANT TO KNOW")

which had me wondering if there's not a means for a misc. python 
program to write to the Mac OS X console?   That would be much nicer 
than having to open up the error log and inspect stuff, as then I 
could see debug info stream past on a console window.


(This is console, as in the "console" run from 
/Applications/Utilities, not the bash "Terminal")


Any help or suggestions appreciated. Thx.

Ross.


Yeah, use the syslog facility, for instance:

import syslog
syslog.openlog("django")
syslog.syslog(syslog.LOG_ALERT, "Here is my syslog alert message")


It seems that anything below alert level isn't shown in the console.
I don't how to change this.


You might want to consider using the Python logging module instead?

--irmen

--
http://mail.python.org/mailman/listinfo/python-list


Blanket font setting?

2008-09-18 Thread RGK
I'm doing an app with the AUI manager capabilities (using some of the 
wxPython demo's for help).  All is well, except I'm a bit disappointed 
with the font management.


The default font for all the widgets (TextCtrl's, StaticText's etc) are 
a bit large for my design intent and as I try to adjust that, it appears 
I have to do a


  font = wx.SystemSettings_GetFont(wx.SYS_SYSTEM_FONT)
  font.SetPointSize(9)

Then, for every texty thing:

  myTextThing.setfont(font)
  someOtherThing.setfont(font)

Thus if I have hundreds of texty things, I've got to assign that 
hundreds of times.


Is there any sort of blanket font setting, perhaps like:

  wx.SystemSettings_SetFont(font)   #this doesn't exist

that could set everything with one fell swoop?

Thanks for your attention...

Ross.
--
http://mail.python.org/mailman/listinfo/python-list