473,490 Members | 2,458 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

need something like threads, or just multiple simulateous exec's

Hi all,

I'm new in python coding...
Can someone point me how can I do such a usual thing? I need to ping
some hosts, and harvest results in one list. Just cause there is about
120 hosts in the LAN, and approx. 10% is down at working hours, pinging
each host at a time from cycle is VERY slow solution.
I did:
retv = os.popen('ping -c 1 ' + ip + ' |grep \"1 packet\" |awk \'{print
$4}\'')
res = retv.read()

In cycle. BUT. I need something FASTER. And I feel fear to scary word
'thread'. May someone give me some examle using threads for this
purpose? Thanks.
Best wishes,
~ Serge.

Jul 18 '05 #1
2 1709
Serge A. Ribalchenko wrote:
Hi all,

I'm new in python coding...
Can someone point me how can I do such a usual thing? I need to ping
some hosts, and harvest results in one list. Just cause there is about
120 hosts in the LAN, and approx. 10% is down at working hours, pinging
each host at a time from cycle is VERY slow solution.
I did:
retv = os.popen('ping -c 1 ' + ip + ' |grep \"1 packet\" |awk \'{print
$4}\'')
res = retv.read()
In cycle. BUT. I need something FASTER. And I feel fear to scary word
'thread'. May someone give me some examle using threads for this
purpose? Thanks.
Best wishes,
~ Serge.


import threading
import Queue
import os

output = Queue.Queue()

def f(ip, output=output):
output.put(os.popen('ping -c 1 ' + ip + ' |grep \"1 packet\" \
|awk\'{print > $4}\'')
for ip in host:
threading.Thread(target=f, args=(ip,)).start()

for ip in host:
print output.get()
- Josiah
Jul 18 '05 #2
I'm not sure why you need to use threads here. popen()ed processes
execute asynchronously, blocking when they create enough unread output.
In this case, they'll generate very little output. Why not try this?

# Create all processes at once
pipes = [os.popen(...) for ip in hosts]

# Read their output
output = [pipe.read for pipe in pipes]

Suppose you decide you want to run only N os.popen()s at a time. Here
you'll want to use select() instead of threads. Something like this:

remaining_hosts = hosts[:]
pipes = []
output = []
for i in range(nthreads):
while remaining_hosts or pipes:
while remaining_hosts and len(pipes) < npipes:
host = remaining_hosts.pop()
pipes.append(os.popen(...))
ready = select.select([pipes], [], [])[0]
for pipe in ready:
# Assuming output arrives all at once
# (it should since it's one line)
output.append(pipe.read())
pipes.remove(pipe)
pipe.close()

Jeff

Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1774
by: Anastasios Kotsikonas | last post by:
Hi all, here's the situtation: 1) JDK 1.4.2_02 & Tomcat 4.1.x (no difference with any 1.4.x) 2) Multiple threads (testing with 5) access non-sync methods in a single instance of a class...
8
1591
by: Brad Tilley | last post by:
I have a function that starts a socket server looping continuously listening for connections. It works. Clients can connect to it and send data. The problem is this: I want to get the data that...
6
4957
by: James Radke | last post by:
Hello, I have a multithreaded windows NT service application (vb.net 2003) that I am working on (my first one), which reads a message queue and creates multiple threads to perform the processing...
10
1669
by: HK | last post by:
With VB.NET 2005, and a Windows Form, running on a dual CPU box, I need to take a recordset (e.g. 100,000 records) and spawn a thread to handle an internet XML transaction routine for each of the...
7
3087
by: Michael | last post by:
I'm writing an application that decodes a file containing binary records. Each record is a particular event type. Each record is translated into ASCII and then written to a file. Each file contains...
14
5487
by: mistral | last post by:
Need compile python code, source is in html and starts with parameters: #!/bin/sh - "exec" "python" "-O" "$0" "$@" I have installed ActivePython for windows.
35
9297
by: keerthyragavendran | last post by:
hi i'm downloading a single file using multiple threads... how can i specify a particular range of bytes alone from a single large file... for example say if i need only bytes ranging from...
5
2019
by: bean330 | last post by:
Hey, I'm somewhat new to C# and I need a little help, please! I'm selecting a bunch of records, setting properties on a COM executable and then calling a method on that executable to run. I...
58
8013
by: bonneylake | last post by:
Hey Everyone, Well recently i been inserting multiple fields for a section in my form called "serial". Well now i am trying to insert multiple fields for the not only the serial section but also...
0
7108
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
6967
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...
0
7142
Oralloy
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,...
0
7181
jinu1996
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...
1
6847
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
7352
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...
1
4875
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3071
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
272
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.