Hello everyone,
I am having a tiny problem putting together a small script. I tried throwing together a multi-threaded ping utility, but the threads are not behaving the way I anticipated...Perhaps one of you could look and see why this is happening.
Python 2.5.2, Ubuntu 8.04 -
#! /usr/bin/env python
-
-
import os, sys, threading
-
-
class PingThread(threading.Thread):
-
host = ''
-
-
def __init__(self, host):
-
self.host = host
-
threading.Thread.__init__(self)
-
-
def run (self):
-
os.execv('/bin/ping', ['/bin/ping', self.host])
-
-
print "PingThread('google.com').start()"
-
PingThread('google.com').start()
-
print "PingThread('yahoo.com').start()"
-
PingThread('yahoo.com').start()
-
print "Main thread ended."
-
If someone could point out my misuse of the Thread class, I would appreciate it.
Thanks,
Motoma
2 4131
It appears that my problem is not with the utilization of the Thread class; rather, my error was with my misunderstanding of the way python's exec*() functions work.
In C, one way to spawn a new process is to fork() and exec*() in the new thread. However, when running exec*() commands in python, it appears that the entire python process is taken over by the exec*() command, not just that particular thread. My solution to this issue is to utilize the os.system() call, rather than the os.exec*() call.
Working example: -
#! /usr/bin/env python
-
-
import os, sys, threading
-
-
class PingThread(threading.Thread):
-
host = ''
-
-
def __init__(self, host):
-
self.host = host
-
threading.Thread.__init__(self)
-
-
def run(self):
-
os.system('/bin/ping %s' %(self.host))
-
-
print "PingThread('google.com').start()"
-
PingThread('google.com').start()
-
print "PingThread('yahoo.com').start()"
-
PingThread('yahoo.com').start()
-
print "Main thread ended."
-
It appears that my problem is not with the utilization of the Thread class; rather, my error was with my misunderstanding of the way python's exec*() functions work.
In C, one way to spawn a new process is to fork() and exec*() in the new thread. However, when running exec*() commands in python, it appears that the entire python process is taken over by the exec*() command, not just that particular thread. My solution to this issue is to utilize the os.system() call, rather than the os.exec*() call.
[/code]
Actually, these work the same in both C and Python. You are confusing threads and processes. The fork() system call creates a new process, you then must determine which process is the child, and explicitly replace the current program using one of the exec* functions. If you simply launch a new thread, the thread is running as part of the same process, so it will naturally take over the entire process when you call an exec* function. The important thing to understand here is that a Thread is a different construct than a process.
The os.system() call is nice and easy, however, if you want to set up special inter-process communication, you should really be using the fork(), exec*() and potentially dup2(). dup2() allows you to map a file descriptor to another. For example, if you want the new process to read from the parent process instead of stdin, you need to remap its stdin file descriptor to a pipe from the parent process using dup2() before launching the program.
Example (grep data in parent): -
#include os
-
#include sys
-
-
(pipe_read, pipe_write) = os.pipe()
-
-
pid = os.fork()
-
if pid: #parent process
-
os.close( pipe_write )
-
os.dup2( pipe_read, sys.stdin.fileno() )
-
os.execv('/bin/grep', ['/bin/grep', expression])
-
else: #parent process
-
os.close( pipe_read )
-
os.write( pipe_write, text )
-
The above example is not complete, you would need a way to retrieve the data from the child process, potentially via another pipe that redirects its stdout.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Daniel Bernhardt |
last post by:
Hello,
my thread calls a program with os.system().
> os.system("/usr/bin/wine /path/to/ultima/online.exe")
Ultima Online is starting and i can enter commands and navigate through the
game with...
|
by: M Herriott |
last post by:
Hello to all -
#1: I would have rather used an AND with the choice of newsgroups. I
would like to talk to those who subscribe to comp.lang.c++ AND
comp.unix.solaris - but I'm stuck with OR....
|
by: New |
last post by:
How can I uses a pipe to get the output from a execv() and redirect it to a
parent process?
in ansi c if possible
Thank-you
|
by: Douglas Dillon |
last post by:
I am trying to use tiffcp to reconfigure tif files (if they are tiled)
to nontiled so I can use existing code to handle them. However, I am
having issues providing the arguments. The app is run...
|
by: Nico Kruger |
last post by:
I want to execute a command (in this case, and it seems to be
significant, a Java program) in a thread in Python. When I execute the
java binary in the main python thread, everything runs...
|
by: martin paul |
last post by:
Sir please consider the following....
I would like to copy contents from one file (mart1.c) to another file (mart2.c) using execv() function (using command line arguments).....
The command...
|
by: lgomez8801 |
last post by:
lgomez8801
Newbie
1 Posts Today
07:37 PM
#1
C language "execv" question
--------------------------------------------------------------------------------
|
by: CMorgan |
last post by:
Hi everybody,
I am experiencing an annoying problem with fork() and execv().
In my program I need to launch the "pppd" from a thread, so, I create
a new process with fork and then in the child...
|
by: dudeja.rajat |
last post by:
Hi,
I'm facing problem with the execv command:
my command is :
os.execv(' C:\Program Files\Subversion\bin\svn ', ( 'list', ' \"
http://subversion.stv.abc.com/svn/Eng \" ' ) )
The error I'm...
|
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
|
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...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
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: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
| |