How to prevent from race conditions to share data between many process and thread in python

2007-02-06 Thread mars
I use TurboGears to do some web service. TurboGears use cherrypy. When
web browser access this site, the cherrypy will call my python
program. So my program looks like a lib. When web browser access the
site, the http server will fock a process or gerenate a thread. I need
share some data or operate some files. How can I prevent from race
conditions. Is there any way can I lock this.
Thank you in advance!

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


Re: How to prevent from race conditions to share data between many process and thread in python

2007-02-06 Thread mars
On 2月6日, 下午6时14分, "Diez B. Roggisch" <[EMAIL PROTECTED]> wrote:
> mars wrote:
> > I use TurboGears to do some web service. TurboGears use cherrypy. When
> > web browser access this site, the cherrypy will call my python
> > program. So my program looks like a lib. When web browser access the
> > site, the http server will fock a process or gerenate a thread. I need
> > share some data or operate some files. How can I prevent from race
> > conditions. Is there any way can I lock this.
> > Thank you in advance!
>
> There are the Lock and RLock objects available in the module threading.
>
> Diez

Can this also lock mutil-process?

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

Read Binary data

2008-09-04 Thread Mars creature
Hi guys,
  I am trying to read a binary file created by the following matlab
command:
fid=fopen('a.bin','w','b'); fwrite(fid,a,'real*8'); fclose(fid);, and
wondering how to do it in Python. I googled it but still get
confused.
  'b' in fopen is for 'big-endian', 'real*8' in fwrite is for 64bit
float.
 Thank you very much!
Jinbo Wang
--
http://mail.python.org/mailman/listinfo/python-list


Re: Read Binary data

2008-09-04 Thread Mars creature
On Sep 4, 12:03 pm, Fredrik Lundh <[EMAIL PROTECTED]> wrote:
> "Mars creature" wrote:
> >   I am trying to read a binary file created by the following matlab
> > command:
> > fid=fopen('a.bin','w','b'); fwrite(fid,a,'real*8'); fclose(fid);, and
> > wondering how to do it in Python. I googled it but still get
> > confused.
> >   'b' in fopen is for 'big-endian', 'real*8' in fwrite is for 64bit
> > float.
>
> f = open("a.bin", "rb") # read binary data
> s = f.read() # read all bytes into a string
>
> import array, sys
>
> a = array.array("f", s) # "f" for float
> if sys.byteorder != "big":
> a.byteswap()
>
> 

Thanks Fredrik! I appreciate it!
The only thing is that a = array.array("f", s) should be a =
array.array("d", s) as the data is double precision.
Thanks again!
--
http://mail.python.org/mailman/listinfo/python-list


Read and write binary data

2008-09-07 Thread Mars creature
Hi guys,
  I am new to Python, and thinking about migrating to it from matlab
as it is a really cool language. Right now, I am trying to figure out
how to control read and write binary data, like
'formatted','stream','big-endian','little-edian' etc.. as in fortran.
I googled, but can not find a clear answer. Anyone has clue where can
I learn it? Thanks!!
Jinbo
--
http://mail.python.org/mailman/listinfo/python-list


Re: Read and write binary data

2008-09-08 Thread Mars creature
On Sep 7, 8:55 pm, Patrick Maupin <[EMAIL PROTECTED]> wrote:
> On Sep 7, 5:41 pm, Mars creature <[EMAIL PROTECTED]> wrote:
>
> > Hi guys,
> >   I am new to Python, and thinking about migrating to it from matlab
> > as it is a really cool language. Right now, I am trying to figure out
> > how to control read and write binary data, like
> > 'formatted','stream','big-endian','little-edian' etc.. as in fortran.
> > I googled, but can not find a clear answer. Anyone has clue where can
> > I learn it? Thanks!!
> > Jinbo
>
> Start by looking at the array module.
>
> Regards,
> Pat

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


How to pass a global variable to a module?

2009-09-29 Thread Mars creature
Dear Python users,
  I just start to use python and love this language. I met this
problem when I try to save my functions in a separate file.
The question is how I can pass a global variable to a function which
is saved in another file. If I save the function I defined in the same
file with the main program, there is no problem after I declare the
global variable. But problem comes out when I save all the function is
a separate file. Help is very much appreciated! Thanks!
Jinbo
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to pass a global variable to a module?

2009-09-29 Thread Mars creature
On Sep 29, 12:49 pm, "Rami Chowdhury" 
wrote:
> On Tue, 29 Sep 2009 09:40:29 -0700, Mars creature  wrote:
> > Dear Python users,
> >   I just start to use python and love this language. I met this
> > problem when I try to save my functions in a separate file.
> > The question is how I can pass a global variable to a function which
> > is saved in another file. If I save the function I defined in the same
> > file with the main program, there is no problem after I declare the
> > global variable. But problem comes out when I save all the function is
> > a separate file. Help is very much appreciated! Thanks!
> > Jinbo
>
> In Python, as in many other languages, I'd advise that you think about  
> whether your variable needs to be global, or whether you could (or should)  
> simply pass the variable to the function as a parameter.
>
> HTH,
> Rami
>
> --
> Rami Chowdhury
> "Never attribute to malice that which can be attributed to stupidity" --  
> Hanlon's Razor
> 408-597-7068 (US) / 07875-841-046 (UK) / 0189-245544 (BD)

Thank you guys for the prompt and helpful response.
>From the link Gregor posted, it seems no way to share variable between
modules.

I can understand the point that global variables tends to mess up
programs.

Assume that I have 10 parameters need to pass to the function. If
these parameters are fixed, I can use another module to store these 10
parameters, and import to the module, as suggested by jean-michel. But
what if these 10 parameters will be changed in the main program?
Passing the variable to the function as a parameter suggested by Rami
will certainly do, but I am wondering weather there are other ways.
What you'd like to code it?
Thank you very much!
Jinbo
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to pass a global variable to a module?

2009-09-30 Thread Mars creature
On Sep 30, 5:31 am, Jean-Michel Pichavant 
wrote:
> Mars creature wrote:
> > On Sep 29, 12:49 pm, "Rami Chowdhury" 
> > wrote:
>
> >> On Tue, 29 Sep 2009 09:40:29 -0700, Mars creature  wrote:
>
> >>> Dear Python users,
> >>>   I just start to use python and love this language. I met this
> >>> problem when I try to save my functions in a separate file.
> >>> The question is how I can pass a global variable to a function which
> >>> is saved in another file. If I save the function I defined in the same
> >>> file with the main program, there is no problem after I declare the
> >>> global variable. But problem comes out when I save all the function is
> >>> a separate file. Help is very much appreciated! Thanks!
> >>> Jinbo
>
> >> In Python, as in many other languages, I'd advise that you think about  
> >> whether your variable needs to be global, or whether you could (or should) 
> >>  
> >> simply pass the variable to the function as a parameter.
>
> >> HTH,
> >> Rami
>
> >> --
> >> Rami Chowdhury
> >> "Never attribute to malice that which can be attributed to stupidity" --  
> >> Hanlon's Razor
> >> 408-597-7068 (US) / 07875-841-046 (UK) / 0189-245544 (BD)
>
> > Thank you guys for the prompt and helpful response.
> > >From the link Gregor posted, it seems no way to share variable between
> > modules.
>
> > I can understand the point that global variables tends to mess up
> > programs.
>
> > Assume that I have 10 parameters need to pass to the function. If
> > these parameters are fixed, I can use another module to store these 10
> > parameters, and import to the module, as suggested by jean-michel. But
> > what if these 10 parameters will be changed in the main program?
> > Passing the variable to the function as a parameter suggested by Rami
> > will certainly do, but I am wondering weather there are other ways.
> > What you'd like to code it?
> > Thank you very much!
> > Jinbo
>
> Why don't you post the function you're trying to code, with the
> parameter names ?
> Write the documentation for that function, write what it is supposed to
> do, the parameters, their purpose and the returned value. Just by doing
> this, you may be able to find all by yourself what should be the correct
> function prototype.
>
> JM

The function I am trying to code is quite simple and nothing special.
I guess what I wanted to say was how to avoid typing all parameters
everytime I am using the function. I used to use common block in
Fortran to keep the frequently used data. I could've put all
parameters in a file and import it, if they are unchangable. But in my
case the parameters are changing.

Allow me to say, unpacking the list or dictionary is the answer I
wanted, although this is too trivial for some of you.

Based on the discussion (correct me if I'm wrong),
1, try to avoid global,
2, if parameters are constant, put them in a tuple/list/dictionary and
import them
3, if parameters are changeable, pack them into a list/dictionary and
use *params (for list) or **params (for dict) to unpack and pass to
the function.

I want to thank you all! It's quite bit learning for me from your
discussion.
Jinbo
-- 
http://mail.python.org/mailman/listinfo/python-list