parallel/concurrent process in python

O

Ommer.Simjee

I'm trying to figure out parallel process python code. Something
similar to fork funtion in C.

For example, I using sniff tool in scapy to capture packets but i want
this to run in the background:

-------
from scapy.all import *
import subprocess
import netsnmp

pkts=sniff(iface="eth0",filter="UDP",count=100) # This should run in
the background

print "Next Code."'
---------


"Next Code." should be printed out right away and does not have to
wait for pkts=sniff(...) to finish.

Any ideas?

Thanks,
Ommer S.
 
M

Minesh Patel

I'm trying to figure out parallel process python code. Something
similar to fork funtion in C.

For example, I using sniff tool in scapy to capture packets but i want
this to run in the background:

-------
from scapy.all import *
import subprocess
import netsnmp

pkts=sniff(iface="eth0",filter="UDP",count=100) # This should run in
the background

print "Next Code."'
---------


"Next Code." should be printed out right away and does not have to
wait for pkts=sniff(...) to finish.

Any ideas?

Why not use os.fork(), it is the same as C's fork?

if os.fork(): # Returns 0 to child, non-zero to parent
# Do parent stuff
else:
# Do child stuff
 
C

cgoldberg

Why not use os.fork(), it is the same as C's fork?

os.fork is not cross platform. It is *nix only. Subprocess runs on
Windows also. The OP never specified his platform.

-Corey
 
O

Ommer.Simjee

Why not use os.fork(), it is the same as C's fork?

if os.fork(): # Returns 0 to child, non-zero to parent
  # Do parent stuff
else:
  # Do child stuff

Thanks,It works perfectly.
 
M

Minesh Patel

os.fork is not cross platform.  It is *nix only.  Subprocess runs on
Windows also.  The OP never specified his platform.

Just out of curiosity, how is one able to replace an os.fork() call
with subprocess and have the child execute multiple statements? I
typically see subprocess used for spawning a shell command, piping,
etc...
 

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,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top