spawn or fork

Discussion in 'Python' started by C Gillespie, Dec 8, 2004.

  1. C Gillespie

    C Gillespie Guest

    Dear All,

    I have a function
    def printHello():
    fp = open('file','w')
    fp.write('hello')
    fp.close()

    I would like to call that function using spawn or fork. My questions are:

    1. Which should I use
    2. How do I call that function if it is defined in the same file.

    Many thanks

    Colin
     
    C Gillespie, Dec 8, 2004
    #1
    1. Advertising

  2. C Gillespie

    Miki Tebeka Guest

    Hello Colin,

    > I have a function
    > def printHello():
    > fp = open('file','w')
    > fp.write('hello')
    > fp.close()
    >
    > I would like to call that function using spawn or fork. My questions are:
    >
    > 1. Which should I use

    spawn and fork are very different functions. Read the documentation on
    each.
    Note the "fork" is available only in Unix like systems.

    > 2. How do I call that function if it is defined in the same file.

    Just call it.

    def foo():
    print 1

    foo()

    Bye.
    --
    ------------------------------------------------------------------------
    Miki Tebeka <>
    http://tebeka.bizhat.com
    The only difference between children and adults is the price of the toys
     
    Miki Tebeka, Dec 8, 2004
    #2
    1. Advertising

  3. C Gillespie

    C Gillespie Guest

    "Miki Tebeka" <> wrote in message
    news:...
    > Hello Colin,
    >
    > > I have a function
    > > def printHello():
    > > fp = open('file','w')
    > > fp.write('hello')
    > > fp.close()
    > >
    > > I would like to call that function using spawn or fork. My questions

    are:
    > >
    > > 1. Which should I use

    > spawn and fork are very different functions. Read the documentation on
    > each.
    > Note the "fork" is available only in Unix like systems.
    >
    > > 2. How do I call that function if it is defined in the same file.

    > Just call it.
    >
    > def foo():
    > print 1
    >

    Thanks, but can I call it using spawn?
     
    C Gillespie, Dec 8, 2004
    #3
  4. C Gillespie

    Eric Brunel Guest

    C Gillespie wrote:
    > Dear All,
    >
    > I have a function
    > def printHello():
    > fp = open('file','w')
    > fp.write('hello')
    > fp.close()
    >
    > I would like to call that function using spawn or fork. My questions are:
    >
    > 1. Which should I use
    > 2. How do I call that function if it is defined in the same file.


    spawn execute an external executable program *outside* your current script,
    *not* a Python function. So say you want to run wordpad.exe from your Python
    script, you could do:

    os.spawn(os.P_NOWAIT, "C:\\Program files\\Accesories\\wordpad.exe, [...])

    So you *need* an external executable to be passed to spawn.

    fork works another way: it duplicates the context of your process in another one
    and continues both processes in parallel. So basically, it doesn't *execute*
    anything, but just creates a process. You may then call your function is the new
    process (a.k.a the "child" process):

    def printHello():
    ...
    if os.fork() == 0:
    ## fork returns 0 in the process copy => this is where we call our function
    printHello()
    else:
    ## If fork doesn't return 0, we're in the original => other code
    ...

    However, fork is only available on Unices.

    What are you trying to do exactly? If you provide more explanations, we may
    provide a better help than the simplistic one above.

    HTH
    --
    - Eric Brunel <eric (underscore) brunel (at) despammed (dot) com> -
    PragmaDev : Real Time Software Development Tools - http://www.pragmadev.com
     
    Eric Brunel, Dec 8, 2004
    #4
  5. > Thanks, but can I call it using spawn?

    No, but you can spawn a python interpreter that calls it. Like this:

    spawnv(P_<whatever>, sys.executable, ['-c', 'import myfile; foo()'])

    But I suggest you use fork - then you can call it in the interpreter itself.
    --
    Regards,

    Diez B. Roggisch
     
    Diez B. Roggisch, Dec 8, 2004
    #5
  6. C Gillespie

    C Gillespie Guest


    > What are you trying to do exactly? If you provide more explanations, we

    may
    > provide a better help than the simplistic one above.

    Dear All,

    Thanks for the suggestions.

    Basically, I have the situation where a user (via the web) requests data
    from a database that has to be written to file. However, this takes a couple
    of minutes. So the way I thought of doing this is:
    1. create an empty file.
    2a. tell the user where to look for the file.
    2b. spawn a process to insert data into the file.

    This way the user can see the data as its being written.

    Thanks

    Colin
     
    C Gillespie, Dec 8, 2004
    #6
  7. C Gillespie

    Eric Brunel Guest

    C Gillespie wrote:
    >>What are you trying to do exactly? If you provide more explanations, we

    >
    > may
    >
    >>provide a better help than the simplistic one above.

    >
    > Dear All,
    >
    > Thanks for the suggestions.
    >
    > Basically, I have the situation where a user (via the web) requests data
    > from a database that has to be written to file. However, this takes a couple
    > of minutes. So the way I thought of doing this is:
    > 1. create an empty file.
    > 2a. tell the user where to look for the file.
    > 2b. spawn a process to insert data into the file.
    >
    > This way the user can see the data as its being written.


    You may want to use threads for this instead of processes. See
    http://docs.python.org/lib/module-threading.html

    HTH
    --
    - Eric Brunel <eric (underscore) brunel (at) despammed (dot) com> -
    PragmaDev : Real Time Software Development Tools - http://www.pragmadev.com
     
    Eric Brunel, Dec 8, 2004
    #7
  8. C Gillespie

    Guest

    Eric Brunel wrote:
    > > Basically, I have the situation where a user (via the web) requests

    data
    > > from a database that has to be written to file. However, this takes

    a couple
    > > of minutes. So the way I thought of doing this is:
    > > 1. create an empty file.
    > > 2a. tell the user where to look for the file.
    > > 2b. spawn a process to insert data into the file.
    > >
    > > This way the user can see the data as its being written.

    >
    > You may want to use threads for this instead of processes. See
    > http://docs.python.org/lib/module-threading.html


    I haven't seen any reason he wants to give up protected memory, so the
    only reason to use threads is if the platform doesn't support fork (or
    has a broken/nonperformant implementation thereof).
     
    , Dec 8, 2004
    #8
    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. Lingyun Yang
    Replies:
    4
    Views:
    11,913
    Keith Dart
    Dec 16, 2004
  2. Derek Basch
    Replies:
    2
    Views:
    1,327
    Donn Cave
    Jan 21, 2005
  3. Sabre
    Replies:
    1
    Views:
    334
    Stefan Behnel
    Mar 21, 2006
  4. Ed Hames
    Replies:
    0
    Views:
    397
    Ed Hames
    Apr 16, 2008
  5. Edgardo Hames
    Replies:
    1
    Views:
    381
    Ed Hames
    May 6, 2008
Loading...

Share This Page