By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,733 Members | 940 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,733 IT Pros & Developers. It's quick & easy.

spawn or fork

P: n/a
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
Jul 18 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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 <mi*********@zoran.com>
http://tebeka.bizhat.com
The only difference between children and adults is the price of the toys
Jul 18 '05 #2

P: n/a

"Miki Tebeka" <mi*********@zoran.com> wrote in message
news:ma**************************************@pyth on.org...
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?

Jul 18 '05 #3

P: n/a
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
Jul 18 '05 #4

P: n/a
> 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
Jul 18 '05 #5

P: n/a
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
Jul 18 '05 #6

P: n/a
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
Jul 18 '05 #7

P: n/a
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).

Jul 18 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.