calling functions at the same time

Discussion in 'Python' started by bart_nessux, Apr 30, 2004.

  1. bart_nessux

    bart_nessux Guest

    I need a script to call several functions at the same time. How does one
    call more than one function simultaneously?
    bart_nessux, Apr 30, 2004
    #1
    1. Advertising

  2. bart_nessux

    Guest

    bart_nessux wrote:
    >
    > I need a script to call several functions at the same time. How does one
    > call more than one function simultaneously?


    You can call them pseudo-concurrently by starting a new thread for each
    one. You cannot call them truly concurrently unless you have a separate
    CPU for each function.

    Mitchell Timin

    --
    "Many are stubborn in pursuit of the path they have chosen, few in
    pursuit of the goal." - Friedrich Nietzsche

    http://annevolve.sourceforge.net is what I'm into nowadays.
    Humans may write to me at this address: zenguy at shaw dot ca
    , Apr 30, 2004
    #2
    1. Advertising

  3. In article <c6udda$qpv$>,
    bart_nessux <> wrote:
    >I need a script to call several functions at the same time. How does one
    >call more than one function simultaneously?
    >
    >
    >


    This has several smart-alecky answers, including "you don't",
    and "with co-routines". The best way you can help yourself is
    to describe a concrete situation where your (Python?) script
    has a need to call even two functions simultaneously. It turns
    out "simultaneously" has a plethora of meanings, and you're the
    only one in a position to get others to understand which you
    have in mind.

    It'll also help to know whether you mean "function" as specific
    to the Python language, or more abstractly, as a unit of useful
    accomplishment.
    --

    Cameron Laird <>
    Business: http://www.Phaseit.net
    Cameron Laird, Apr 30, 2004
    #3
  4. bart_nessux

    Bart Nessux Guest

    Cameron Laird wrote:

    > In article <c6udda$qpv$>,
    > bart_nessux <> wrote:
    >>I need a script to call several functions at the same time. How does one
    >>call more than one function simultaneously?
    >>
    >>
    >>

    >
    > This has several smart-alecky answers, including "you don't",
    > and "with co-routines". The best way you can help yourself is
    > to describe a concrete situation where your (Python?) script
    > has a need to call even two functions simultaneously.


    I need to ping 4 hosts at exactly the same time from the same machine (I
    plan to timestamp the pings) to test and measure network conditions over
    different routes to different hosts. Putting all the ping hosts in a list
    and looping through it is not a fair or balanced way to do this because of
    the time differences.



    > It turns
    > out "simultaneously" has a plethora of meanings, and you're the
    > only one in a position to get others to understand which you
    > have in mind.


    I mean it to mean: at the *exact* same time... concurrently. Like runners
    starting a race together.

    >
    > It'll also help to know whether you mean "function" as specific
    > to the Python language, or more abstractly, as a unit of useful
    > accomplishment.


    Specific to Python.
    Bart Nessux, May 1, 2004
    #4
  5. bart_nessux

    Terry Reedy Guest

    "Bart Nessux" <> wrote in message
    news:c6unuq$d0f$...
    > I need to ping 4 hosts at exactly the same time from the same machine (I
    > plan to timestamp the pings) to test and measure network conditions over
    > different routes to different hosts. Putting all the ping hosts in a list
    > and looping through it is not a fair or balanced way to do this because

    of
    > the time differences.


    If you do 24 trials, one for each permutation, the result would be about as
    fair and balanced as you can get.

    Terry J. Reedy
    Terry Reedy, May 1, 2004
    #5
  6. bart_nessux

    asdf sdf Guest

    bart_nessux wrote:
    > I need a script to call several functions at the same time. How does one
    > call more than one function simultaneously?
    >
    >
    >

    i just added my first thread to a wxpython program today. simple. in
    fact my first threaded program of any kind.

    as indicated, threads might help you. you didn't mention your platform
    but probably it has real thread support. nevertheless, this might not
    be for you if you are running in a Unix environment with a
    multiprocessing (forking) program.

    you launch your threads:

    import thread

    # create a lock you can use to block the threads until you are ready
    mymutex = thread.allocate_lock()

    def MyConcurrentFunction():
    # important concurrent stuff
    pass

    def ThreadThing():
    # thread will hang on 'acquire' until mutex is released
    mymutex.acquire()
    # release lock for the next thread
    mymutex.release()
    # now do your stuff
    MyConcurrentFunction()

    mymutex.acquire()
    # launch your threads
    thread.start_new_thread(ThreadThing, ())
    thread.start_new_thread(ThreadThing, ())
    thread.start_new_thread(ThreadThing, ())...
    # your threads are all hung on the 'acquire' statement
    # release the lock, unblocking the threads
    mymutex.release()
    # now all your threads are grabbing and releasing the mutex

    # all done

    I just made that whole thing up. But i think that's more or less how
    you launch threads.

    you have to consider that the thread may run as soon as you launch it.
    each thread could complete your special function before the rest of the
    threads get launched.

    i think you address that problem by creating a mutex or semaphore that
    each thread must acquire before it can proceed. That is, the thread
    will block (hang) waiting for the mutex to become available. (think of
    a mutex as an abstract lock resource). when all your threads are
    launched, the main program can release the mutex(es), unblocking the
    threads. The threads will unhang, and do your concurrent thing, more or
    less concurrently.

    Understand that concurrency is not guaranteed (as i understand it). the
    OS controls which thread runs when, and it is not deterministic.

    you could use an list of mutexes, to get an iota more concurrency.

    all this i learned this very day from Programming Python, pg 104. so
    take my advice with a shovel of salt.

    but it sounds like the way to go to me.
    asdf sdf, May 1, 2004
    #6
  7. In article <c6unuq$d0f$>,
    Bart Nessux <> wrote:
    .
    .
    .
    >I need to ping 4 hosts at exactly the same time from the same machine (I
    >plan to timestamp the pings) to test and measure network conditions over
    >different routes to different hosts. Putting all the ping hosts in a list
    >and looping through it is not a fair or balanced way to do this because of
    >the time differences.
    >
    >
    >
    >> It turns
    >> out "simultaneously" has a plethora of meanings, and you're the
    >> only one in a position to get others to understand which you
    >> have in mind.

    >
    >I mean it to mean: at the *exact* same time... concurrently. Like runners
    >starting a race together.
    >
    >>
    >> It'll also help to know whether you mean "function" as specific
    >> to the Python language, or more abstractly, as a unit of useful
    >> accomplishment.

    >
    >Specific to Python.


    Good description! We're definitely getting somewhere.

    How serious are you about this?? More specifically, do
    you have in mind a conventional desktop host, or are you
    investing in specialized hardware and firmware? On a
    conventional rig, your network traffic will be sequenced;
    a single networking interface, say, can't emit four signals
    simultaneously, but *must* do first one packet, then the
    next, then ... Are you OK with that?

    What does "ping" mean to you? Do you have in mind the
    command-line executable, or are you planning to shape your
    own ICMP traffic?
    --

    Cameron Laird <>
    Business: http://www.Phaseit.net
    Cameron Laird, May 1, 2004
    #7
  8. In article <c6unuq$d0f$>, Bart Nessux wrote:

    > I need to ping 4 hosts at exactly the same time from the same machine


    That simply can't be done.

    > (I plan to timestamp the pings) to test and measure network
    > conditions over different routes to different hosts. Putting
    > all the ping hosts in a list and looping through it is not a
    > fair or balanced way to do this because of the time
    > differences.


    Exactly how many packets do you think are allowed on the wire
    at once with Ethernet??

    > I mean it to mean: at the *exact* same time... concurrently. Like runners
    > starting a race together.


    Not physically possible unless you've got 4 cpus with, 4
    separate PCI buses, 4 Ethernet boards on 4 different Ethernet
    segments, and some very special HW/SW to keep things
    synchronized.

    --
    Grant Edwards grante Yow! I'm ANN LANDERS!! I
    at can SHOPLIFT!!
    visi.com
    Grant Edwards, May 1, 2004
    #8
  9. bart_nessux

    asdf sdf Guest

    sigh. i just read what you want to do.

    my answer was so cool too.

    you are doing network diagnostics. it is unlikely that a non-network guy
    (you) can cobble together a few lines of script and develop much in the
    way of detailed diagnostics.

    much better to find someone who already knows networking and use the
    great many highly sophisticated tools that are already available (and free).

    good luck
    asdf sdf, May 1, 2004
    #9
  10. bart_nessux

    Knio Guest

    Bart Nessux wrote:
    > Cameron Laird wrote:
    >
    >
    >>In article <c6udda$qpv$>,
    >>bart_nessux <> wrote:
    >>
    >>>I need a script to call several functions at the same time. How does one
    >>>call more than one function simultaneously?
    >>>
    >>>
    >>>

    >>
    >>This has several smart-alecky answers, including "you don't",
    >>and "with co-routines". The best way you can help yourself is
    >>to describe a concrete situation where your (Python?) script
    >>has a need to call even two functions simultaneously.

    >
    >
    > I need to ping 4 hosts at exactly the same time from the same machine (I
    > plan to timestamp the pings) to test and measure network conditions over
    > different routes to different hosts. Putting all the ping hosts in a list
    > and looping through it is not a fair or balanced way to do this because of
    > the time differences.
    >
    >
    >
    >
    >>It turns
    >>out "simultaneously" has a plethora of meanings, and you're the
    >>only one in a position to get others to understand which you
    >>have in mind.

    >
    >
    > I mean it to mean: at the *exact* same time... concurrently. Like runners
    > starting a race together.
    >
    >
    >>It'll also help to know whether you mean "function" as specific
    >>to the Python language, or more abstractly, as a unit of useful
    >>accomplishment.

    >
    >
    > Specific to Python.



    You can't get greater then 1ms accuracy when measuring a time on todays
    machines, and that will be less when measuring time over a network...
    calling 4 ping functions one after the other will complete in much less
    then 1ms (assuming its asynchronous, or threaded). So, even if you could
    manage to send 4 packets at *exactly* the same time, it would be no more
    accurate.

    not sure how your planning to ping the hosts... check out the asyncore
    and threading modules.
    Knio, May 1, 2004
    #10
  11. In article <kNIkc.1367$U75.564@edtnps89>, Knio wrote:

    > You can't get greater then 1ms accuracy when measuring a time
    > on todays machines, and that will be less when measuring time
    > over a network...


    Not sure what you mean by "todays machines", but on a
    Pentium-class machine running Linux, you get approx 1us
    resolution with calls to time.time():

    import time
    t = []
    for i in range(10):
    t.append(time.time())
    print t

    [1083429542.284164, 1083429542.2841799, 1083429542.2841859,
    1083429542.2841921, 1083429542.284198, 1083429542.284204,
    1083429542.284209, 1083429542.284215, 1083429542.2842209,
    1083429542.2842269]

    The Linux network stack also provides timestamps on the network
    packets with at least 1us resolution.

    > calling 4 ping functions one after the other will complete in much less
    > then 1ms (assuming its asynchronous, or threaded). So, even if you could
    > manage to send 4 packets at *exactly* the same time,


    People, Ethernet is a _serial_ protocol. It allows exactly one
    packet to be transmitted at a time. There is no way to send
    more than one packet at a time.

    > it would be no more accurate.


    --
    Grant Edwards grante Yow! As President I
    at have to go vacuum my coin
    visi.com collection!
    Grant Edwards, May 1, 2004
    #11
  12. bart_nessux

    Peter Hansen Guest

    Grant Edwards wrote:

    > In article <kNIkc.1367$U75.564@edtnps89>, Knio wrote:
    >
    >>You can't get greater then 1ms accuracy when measuring a time
    >>on todays machines, and that will be less when measuring time
    >>over a network...

    >
    > Not sure what you mean by "todays machines", but on a
    > Pentium-class machine running Linux, you get approx 1us
    > resolution with calls to time.time():


    Keep in mind the difference between "accuracy" and
    "resolution" (or "precision").

    I think Knio is probably right that, unless you are
    running on a realtime OS, you won't be able to
    guarantee anything better than 1ms accuracy, and
    quite probably not even that.

    -Peter
    Peter Hansen, May 1, 2004
    #12
  13. In article <>, Peter Hansen wrote:

    >>>You can't get greater then 1ms accuracy when measuring a time
    >>>on todays machines, and that will be less when measuring time
    >>>over a network...

    >>
    >> Not sure what you mean by "todays machines", but on a
    >> Pentium-class machine running Linux, you get approx 1us
    >> resolution with calls to time.time():

    >
    > Keep in mind the difference between "accuracy" and
    > "resolution" (or "precision").


    I do.

    Not only do you get 1us resolution, you get packet timestamp
    _accuracy_ to well under 1ms according to tests I've run
    comparing packet timestamps against an oscilloscope trace.

    Delta-T accuracy of calls to time() are accurate to 1us as
    well. Absolute accuracy depends on how you set your system
    clock. Running NTP with in-kernel FLL control of the system
    tick will generally allow you to maintain absolute accuracies
    of under 100us.

    > I think Knio is probably right that, unless you are running on
    > a realtime OS, you won't be able to guarantee anything better
    > than 1ms accuracy, and quite probably not even that.


    Under Linux, I can guarantee you the answer you get back from
    time.time() is accurate to 1us. However, it is a multitasking
    system, and there are other things running. While it's easy to
    _determine_ exactly when you called time.time(), it's difficult
    to _control_ the point in time when you call time.time(). When
    your task does get a chance to run, and you do get to call
    time.time(), you'll generally get a result accurate to 1us.

    If I do a busy-wait loop with nothing else running, I've been
    able to accurately measure pulsewidths of a few microseconds
    under Linux using the gettimeofday() call (which I believe is
    what time.time() calls.

    As soon as there are other ready tasks, the accuracy of the
    measurement quickly deteriorates to tens of millisconds due to
    scheduling latencies.

    --
    Grant Edwards grante Yow! It's today's SPECIAL!
    at
    visi.com
    Grant Edwards, May 1, 2004
    #13
  14. bart_nessux

    bart_nessux Guest

    Grant Edwards wrote:
    > In article <>, Peter Hansen wrote:
    >
    >
    >>>>You can't get greater then 1ms accuracy when measuring a time
    >>>>on todays machines, and that will be less when measuring time
    >>>>over a network...
    >>>
    >>>Not sure what you mean by "todays machines", but on a
    >>>Pentium-class machine running Linux, you get approx 1us
    >>>resolution with calls to time.time():

    >>
    >>Keep in mind the difference between "accuracy" and
    >>"resolution" (or "precision").

    >
    >
    > I do.
    >
    > Not only do you get 1us resolution, you get packet timestamp
    > _accuracy_ to well under 1ms according to tests I've run
    > comparing packet timestamps against an oscilloscope trace.
    >
    > Delta-T accuracy of calls to time() are accurate to 1us as
    > well. Absolute accuracy depends on how you set your system
    > clock. Running NTP with in-kernel FLL control of the system
    > tick will generally allow you to maintain absolute accuracies
    > of under 100us.
    >
    >
    >>I think Knio is probably right that, unless you are running on
    >>a realtime OS, you won't be able to guarantee anything better
    >>than 1ms accuracy, and quite probably not even that.

    >
    >
    > Under Linux, I can guarantee you the answer you get back from
    > time.time() is accurate to 1us. However, it is a multitasking
    > system, and there are other things running. While it's easy to
    > _determine_ exactly when you called time.time(), it's difficult
    > to _control_ the point in time when you call time.time(). When
    > your task does get a chance to run, and you do get to call
    > time.time(), you'll generally get a result accurate to 1us.
    >
    > If I do a busy-wait loop with nothing else running, I've been
    > able to accurately measure pulsewidths of a few microseconds
    > under Linux using the gettimeofday() call (which I believe is
    > what time.time() calls.
    >
    > As soon as there are other ready tasks, the accuracy of the
    > measurement quickly deteriorates to tens of millisconds due to
    > scheduling latencies.
    >


    Thanks to all for the tips and advice. I experimented with threading...
    it works, but I don't fully understand it so I'm not using it. I ended
    up using ntp on 4 hosts (side by side on the network) to do the pings. I
    wrote some socket server/client scripts so that all four hosts can start
    pinging together. I understand this approach and it's working well.

    Thanks again,
    Bart
    bart_nessux, May 1, 2004
    #14
  15. bart_nessux

    bobb Guest

    "Bart Nessux" <> wrote in message
    news:c6unuq$d0f$...
    > Cameron Laird wrote:
    >
    > > In article <c6udda$qpv$>,
    > > bart_nessux <> wrote:
    > >>I need a script to call several functions at the same time. How does one
    > >>call more than one function simultaneously?
    > >>
    > >>
    > >>

    > >
    > > This has several smart-alecky answers, including "you don't",
    > > and "with co-routines". The best way you can help yourself is
    > > to describe a concrete situation where your (Python?) script
    > > has a need to call even two functions simultaneously.

    >
    > I need to ping 4 hosts at exactly the same time from the same machine (I
    > plan to timestamp the pings) to test and measure network conditions over
    > different routes to different hosts. Putting all the ping hosts in a list
    > and looping through it is not a fair or balanced way to do this because of
    > the time differences.
    >

    The network guy in me says that pinging 4 machines at the same time from 1
    machine is not fair and
    balanced. your network card really only does 1 request at a time... I would
    think that the only fair judge
    is to do it on 4 machines on the same segment, or 4 cards in the same
    machine, otherwise you're bottleneck
    becomes the single network card..
    My opinion...
    bobb

    >
    >
    > > It turns
    > > out "simultaneously" has a plethora of meanings, and you're the
    > > only one in a position to get others to understand which you
    > > have in mind.

    >
    > I mean it to mean: at the *exact* same time... concurrently. Like runners
    > starting a race together.
    >
    > >
    > > It'll also help to know whether you mean "function" as specific
    > > to the Python language, or more abstractly, as a unit of useful
    > > accomplishment.

    >
    > Specific to Python.
    bobb, May 1, 2004
    #15
  16. bart_nessux

    Peter Hansen Guest

    Grant Edwards wrote:

    > In article <>, Peter Hansen wrote:

    (Someone else wrote this first part, then Grant's reply, then mine)
    >>>>You can't get greater then 1ms accuracy when measuring a time
    >>>>on todays machines, and that will be less when measuring time
    >>>>over a network...
    >>>
    >>>Not sure what you mean by "todays machines", but on a
    >>>Pentium-class machine running Linux, you get approx 1us
    >>>resolution with calls to time.time():

    >>
    >>Keep in mind the difference between "accuracy" and
    >>"resolution" (or "precision").

    >
    > I do.
    >
    > Not only do you get 1us resolution, you get packet timestamp
    > _accuracy_ to well under 1ms according to tests I've run
    > comparing packet timestamps against an oscilloscope trace.


    [snip other technical details]

    > As soon as there are other ready tasks, the accuracy of the
    > measurement quickly deteriorates to tens of millisconds due to
    > scheduling latencies.


    The last sentence is really the whole point. The OP needs
    to be aware that the accuracy *cannot* be *guaranteed*
    to better than "tens of milliseconds", overall, regardless
    of the "accuracy" of the clock tick or the resolution of
    anything...

    The notes on timeit.py are helpful in this respect, however:
    taking the shortest result of repeated measurements is a
    pretty good way to get an "accurate" number, most of the
    time. The other, higher results represent the interference
    of all those other tasks which will tend to run as you
    try to take measurements.

    -Peter
    Peter Hansen, May 1, 2004
    #16
  17. bart_nessux

    Knio Guest

    Grant Edwards wrote:
    > In article <kNIkc.1367$U75.564@edtnps89>, Knio wrote:
    >
    >
    >>You can't get greater then 1ms accuracy when measuring a time
    >>on todays machines, and that will be less when measuring time
    >>over a network...

    >
    >
    > Not sure what you mean by "todays machines", but on a
    > Pentium-class machine running Linux, you get approx 1us
    > resolution with calls to time.time():
    >
    > [snip]


    ah, interesting.. on my windows machine (pentium 4) I only have a 1ms
    timer resolution.

    import time
    t = []
    for i in range(10):
    t.append(time.time())
    print t

    [1083451302.5739999, 1083451302.5739999, 1083451302.5739999,
    1083451302.5739999, 1083451302.5739999, 1083451302.5739999,
    1083451302.5739999, 1083451302.5739999, 1083451302.5739999,
    1083451302.5739999]
    Knio, May 1, 2004
    #17
  18. Bart Nessux <> writes:

    > I mean it to mean: at the *exact* same time... concurrently. Like runners
    > starting a race together.


    On how many processors? You need to tell your operating system
    services to schedule the two python funtion calls to each processor
    simultaneously. I don't think anything in Python will let you do that.
    Tor Iver Wilhelmsen, May 2, 2004
    #18
  19. -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    At 2004-04-30T23:43:37Z, Bart Nessux <> writes:

    > I need to ping 4 hosts at exactly the same time from the same machine (I
    > plan to timestamp the pings) to test and measure network conditions over
    > different routes to different hosts. Putting all the ping hosts in a list
    > and looping through it is not a fair or balanced way to do this because of
    > the time differences.


    In other words, you want to emulate the functionality of fping without using
    fping. Is that about right?
    - --
    Kirk Strauser
    The Strauser Group
    Open. Solutions. Simple.
    http://www.strausergroup.com/
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.2.4 (GNU/Linux)

    iD8DBQFAlRaz5sRg+Y0CpvERAt8xAJ4sS+FzCv/Bm57o2b3s3Weh2yTV3gCeM68p
    JVstKJijYWIQxU9+izWk7hQ=
    =zCda
    -----END PGP SIGNATURE-----
    Kirk Strauser, May 2, 2004
    #19
  20. bart_nessux

    Greg Ewing Guest

    Bart Nessux wrote:
    > I need to ping 4 hosts at exactly the same time from the same machine (I
    > plan to timestamp the pings)
    >
    > I mean it to mean: at the *exact* same time.


    Unless your machine has 4 network interfaces, there's no
    way you're going to get the pings sent out at *exactly*
    the same time... :)

    --
    Greg Ewing, Computer Science Dept,
    University of Canterbury,
    Christchurch, New Zealand
    http://www.cosc.canterbury.ac.nz/~greg
    Greg Ewing, May 3, 2004
    #20
    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. Daniel Vallstrom
    Replies:
    2
    Views:
    1,830
    Kevin Bracey
    Nov 21, 2003
  2. flamesrock
    Replies:
    8
    Views:
    434
    Hendrik van Rooyen
    Nov 24, 2006
  3. S?ren Gammelmark
    Replies:
    1
    Views:
    1,866
    Eric Sosman
    Jan 7, 2005
  4. Replies:
    5
    Views:
    331
    Alf P. Steinbach
    Feb 6, 2006
  5. Replies:
    3
    Views:
    1,978
    Steven D'Aprano
    Jul 25, 2009
Loading...

Share This Page