making my extensions work together

Discussion in 'Python' started by Mathew, Aug 4, 2011.

  1. Mathew

    Mathew Guest

    This isn't exactly a Python question but maybe someone here has run into
    this.

    I have 2 extensions and they both access a function in a (static) library.
    The function maintains state information using a static variable.

    This doesn't work. When one of my extensions changes the variable value, the
    other extension does not see the change.

    Would it work if I made my library dynamic?

    This is on Windows XP compiling with MSVC 2008.

    -Mathew
    Mathew, Aug 4, 2011
    #1
    1. Advertising

  2. Mathew

    John Gordon Guest

    In <j1cs2t$j2f$> "Mathew" <> writes:

    > This isn't exactly a Python question but maybe someone here has run into
    > this.


    > I have 2 extensions and they both access a function in a (static) library.
    > The function maintains state information using a static variable.


    I'm not sure what you mean by "extension", but it might not be relevant.

    Are these extensions, whatever they may be, part of the same program?
    Or are they separate?

    > This doesn't work. When one of my extensions changes the variable value, the
    > other extension does not see the change.


    --
    John Gordon A is for Amy, who fell down the stairs
    B is for Basil, assaulted by bears
    -- Edward Gorey, "The Gashlycrumb Tinies"
    John Gordon, Aug 4, 2011
    #2
    1. Advertising

  3. Mathew, 04.08.2011 03:19:
    > This isn't exactly a Python question but maybe someone here has run into
    > this.
    >
    > I have 2 extensions


    With "extensions", I assume you mean extension modules for the CPython
    runtime that are written in C? It would help if you were more specific in
    your problem description.


    > and they both access a function in a (static) library.


    It would help if you mentioned the names of the modules (or packages) and
    of the external library.


    > The function maintains state information using a static variable.


    That's bad design, but it happens.


    > This doesn't work. When one of my extensions changes the variable value, the
    > other extension does not see the change.


    Are the two modules linked in any way or are they just arbitrary modules
    that happen to be installed at the same time, trying to use the same
    external C library?

    If the former, consider letting them communicate with each other by making
    one depend on the other. If you control the source code, you may also
    consider wrapping the library only once and reusing that from the two
    modules. Or, just move the configuration part into a separate module and
    have both depend on that. Or, try to dump the dependency on the static
    variable.

    If the latter, then, well, it depends on several environmental factors that
    you left out in your question.


    > Would it work if I made my library dynamic?


    That's usually a good idea, but it's unlikely to help in this specific case.


    > This is on Windows XP compiling with MSVC 2008.


    Others will know more here.

    Stefan
    Stefan Behnel, Aug 4, 2011
    #3
  4. Mathew

    Mathew Guest

    more info. I have a large 3rd party library with a function that looks like
    this
    void dumbfunc() {
    static int statevar=0;
    ++statevar;
    if (startvar ==3) printf("I have been called 3 times\n");
    }

    and I have 2 extensions, foo,py goo.py,created with SWIG, and the both make
    calls to dumbfunc. In creating the extensions, I linked to the 3rd party
    library.

    The behavior I want to see is
    >foo.dumbfunc()
    >goo.dumbfunc()
    >goo.dumbfunc()

    I have been called 3 times


    "Chris Angelico" <> wrote in message
    news:...
    > On Thu, Aug 4, 2011 at 2:19 AM, Mathew <> wrote:
    >> I have 2 extensions and they both access a function in a (static)
    >> library.
    >> The function maintains state information using a static variable.

    >
    > If your extensions are DLLs and they're both linking to the same
    > static library, you should have two independent copies of that library
    > - which would mean they don't interfere with one another. That's why
    > the other extension doesn't see the change, and normally this is the
    > correct and desired behaviour.
    >
    > Having the two be aware of each other is potentially very messy. As
    > Stefan suggested, making one depend on the other would be a lot
    > simpler. Alternatively, there may be ways to have the two extensions
    > share data through Python itself. What are you looking to accomplish?
    >
    > ChrisA
    Mathew, Aug 4, 2011
    #4
  5. Mathew

    Mathew Guest

    okay. It worked to make my 3'rd party library dynamic. Hopefully this will
    help someone else in the future.


    "Mathew" <> wrote in message
    news:j1cs2t$j2f$...
    > This isn't exactly a Python question but maybe someone here has run into
    > this.
    >
    > I have 2 extensions and they both access a function in a (static) library.
    > The function maintains state information using a static variable.
    >
    > This doesn't work. When one of my extensions changes the variable value,
    > the other extension does not see the change.
    >
    > Would it work if I made my library dynamic?
    >
    > This is on Windows XP compiling with MSVC 2008.
    >
    > -Mathew
    >
    Mathew, Aug 4, 2011
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. zbuilder
    Replies:
    3
    Views:
    533
    Dino Chiesa [MSFT]
    Sep 16, 2003
  2. Piotr
    Replies:
    0
    Views:
    397
    Piotr
    Jan 6, 2006
  3. Daniel Bickett
    Replies:
    2
    Views:
    370
    David Bolen
    Dec 30, 2004
  4. Replies:
    6
    Views:
    296
    John Machin
    Aug 17, 2005
  5. fuli open
    Replies:
    8
    Views:
    314
    Jukka K. Korpela
    May 9, 2007
Loading...

Share This Page