parallel/concurrent process in python

Discussion in 'Python' started by Ommer.Simjee@gmail.com, Mar 9, 2009.

  1. Guest

    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.
     
    , Mar 9, 2009
    #1
    1. Advertising

  2. Minesh Patel Guest

    On Mon, Mar 9, 2009 at 2:47 PM, <> wrote:
    > 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

    --
    Thanks,
    Minesh Patel
     
    Minesh Patel, Mar 10, 2009
    #2
    1. Advertising

  3. cgoldberg Guest

    > 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
     
    cgoldberg, Mar 10, 2009
    #3
  4. Guest

    On Mar 10, 10:06 am, Minesh Patel <> wrote:
    > On Mon, Mar 9, 2009 at 2:47 PM,  <> wrote:
    > > 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
    >
    > --
    > Thanks,
    > Minesh Patel


    Thanks,It works perfectly.
     
    , Mar 10, 2009
    #4
  5. Minesh Patel Guest

    > 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...
    --
    Thanks,
    Minesh Patel
     
    Minesh Patel, Mar 10, 2009
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Pep
    Replies:
    6
    Views:
    875
  2. Soren
    Replies:
    4
    Views:
    1,343
    c d saunter
    Feb 14, 2008
  3. harrismh777
    Replies:
    3
    Views:
    910
  4. Vivek Menon
    Replies:
    5
    Views:
    3,496
    Paul Uiterlinden
    Jun 8, 2011
  5. Vivek Menon
    Replies:
    0
    Views:
    1,806
    Vivek Menon
    Jun 10, 2011
Loading...

Share This Page