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
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to