473,395 Members | 1,608 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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
Jul 18 '05 #1
7 8796
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

"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
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
> 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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Jim Tanner | last post by:
I am trying to start the 2.3.3 IDL and getting an error. I think I might have a dll conflict, but the 2.2.3 IDL works fine. The 2.3.3 IDL used to work just fine. The following are the...
3
by: Mark Roach | last post by:
I am using the daemonize (double-fork) code from the cookbook, and have bumped into a strange issue. This is what my program is doing: def main(): ... useful things ... os.popen('/usr/bin/lp -d...
3
by: Lingyun Yang | last post by:
Hi, I want to use python as a "shell like" program, and execute an external program in it( such as mv, cp, tar, gnuplot) I tried: os.execv("/bin/bash",("/usr/bin/gnuplot",'-c "gnuplot <...
3
by: Sinan Nalkaya | last post by:
hi, i am using os.spawn function, it works well but i need a flag that allows function return the process id with exit/error code, is there any or how can i do it, i can replace spawn with...
9
by: Daniel Nogradi | last post by:
Is it possible to pass a python object to a python program as argument? In my program I would like to start executing an other python program and don't wait until that finishes, only launch it...
1
by: Sabre | last post by:
Hi everybody, how can I spawn or fork a second process onto the second processor of my dual core server ? Is there a possibility to determin the processor on which a process shall run ? ...
2
by: Dmitry Teslenko | last post by:
Hello! How to write portable (win32, unix) script that launches another program and continues its execution? I've looked at spawn*() but it doesn't look in PATH dirs on windows so it's totally...
4
by: Brendan Miller | last post by:
I want to spawn a child process based on an external executable that I have the path for. I then want to wait on that executable, and capture it's output. In the os module, fork is only...
0
by: Jean-Paul Calderone | last post by:
On Fri, 02 May 2008 06:30:03 -0500, Nick Craig-Wood <nick@craig-wood.comwrote: I haven't read all of this thread, so excuse me if this is out of place. There is a good cross-platform solution,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.