spawn or fork

C

C Gillespie

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
 
M

Miki Tebeka

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

C Gillespie

Miki Tebeka said:
Hello Colin,

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

Just call it.

def foo():
print 1
Thanks, but can I call it using spawn?
 
E

Eric Brunel

C said:
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
 
D

Diez B. Roggisch

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

C Gillespie

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
 
E

Eric Brunel

C said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top