spawn or fork

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • C Gillespie

    #1

    spawn or fork

    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


  • Miki Tebeka

    #2
    Re: spawn or fork

    Hello Colin,
    [color=blue]
    > 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[/color]
    spawn and fork are very different functions. Read the documentation on
    each.
    Note the "fork" is available only in Unix like systems.
    [color=blue]
    > 2. How do I call that function if it is defined in the same file.[/color]
    Just call it.

    def foo():
    print 1

    foo()

    Bye.
    --
    ------------------------------------------------------------------------
    Miki Tebeka <miki.tebeka@zo ran.com>

    The only difference between children and adults is the price of the toys

    Comment

    • C Gillespie

      #3
      Re: spawn or fork


      "Miki Tebeka" <miki.tebeka@zo ran.com> wrote in message
      news:mailman.73 59.1102503309.5 135.python-list@python.org ...[color=blue]
      > Hello Colin,
      >[color=green]
      > > 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[/color][/color]
      are:[color=blue][color=green]
      > >
      > > 1. Which should I use[/color]
      > spawn and fork are very different functions. Read the documentation on
      > each.
      > Note the "fork" is available only in Unix like systems.
      >[color=green]
      > > 2. How do I call that function if it is defined in the same file.[/color]
      > Just call it.
      >
      > def foo():
      > print 1
      >[/color]
      Thanks, but can I call it using spawn?



      Comment

      • Eric Brunel

        #4
        Re: spawn or fork

        C Gillespie wrote:[color=blue]
        > 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.[/color]

        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_N OWAIT, "C:\\Progra m files\\Accesori es\\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

        Comment

        • Diez B. Roggisch

          #5
          Re: spawn or fork

          > Thanks, but can I call it using spawn?

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

          spawnv(P_<whate ver>, 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

          Comment

          • C Gillespie

            #6
            Re: spawn or fork

            [color=blue]
            > What are you trying to do exactly? If you provide more explanations, we[/color]
            may[color=blue]
            > provide a better help than the simplistic one above.[/color]
            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


            Comment

            • Eric Brunel

              #7
              Re: spawn or fork

              C Gillespie wrote:[color=blue][color=green]
              >>What are you trying to do exactly? If you provide more explanations, we[/color]
              >
              > may
              >[color=green]
              >>provide a better help than the simplistic one above.[/color]
              >
              > 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.[/color]

              You may want to use threads for this instead of processes. See
              Source code: Lib/threading.py This module constructs higher-level threading interfaces on top of the lower level_thread module. Availability: not WASI. This module does not work or is not available...


              HTH
              --
              - Eric Brunel <eric (underscore) brunel (at) despammed (dot) com> -
              PragmaDev : Real Time Software Development Tools - http://www.pragmadev.com

              Comment

              • sjdevnull@yahoo.com

                #8
                Re: spawn or fork

                Eric Brunel wrote:[color=blue][color=green]
                > > Basically, I have the situation where a user (via the web) requests[/color][/color]
                data[color=blue][color=green]
                > > from a database that has to be written to file. However, this takes[/color][/color]
                a couple[color=blue][color=green]
                > > 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.[/color]
                >
                > You may want to use threads for this instead of processes. See
                > http://docs.python.org/lib/module-threading.html[/color]

                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).

                Comment

                Working...