Python Forum
Timing functions with multiprocessing
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Timing functions with multiprocessing
#1
Hi, I have a function that I execute with no problems with multiprocessing however I cant time it
import multiprocessing as mp
import timeit
poolTimes =  mp.Pool(processes=5)
poolResults = mp.Poool(processes=5)
results = [poolResults.apply(myLibrary.myFunction, args=(myObject,)) for myObject in listMyObjects]
times= [poolTimes.apply(timeit.Timer(lambda: myLibrary.myFunction), args=(myObject,)) for my Object in listMyObjects]
With that code I get: _pickle.PicklingError: Can't pickle attribute lookup inner on timeit failed

If I change it to use timeit.timeit I get: TypeError: 'float' object is not callable

Can you point me to what I am doing wrong?
Reply
#2
Do you want to time how long it takes to finish? The simplest way to do that is to do it within the called function using a start and finish time/datetime.
Reply
#3
(Nov-17-2018, 05:40 PM)woooee Wrote: Do you want to time how long it takes to finish? The simplest way to do that is to do it within the called function using a start and finish time/datetime.
I want to time the function for each of the cases in my list of objects
Reply
#4
I prefer to code these types of things myself
import datetime
import random
import time
from multiprocessing import Process, Manager


def test_f(process_num, time_limit, test_d, ):
    print(process_num, time_limit)

    ## you can set up the dictionary in many different ways
    test_d["start %d" % process_num] = datetime.datetime.now()  ## start time
    ctr=0
    while ctr < time_limit:
        ctr += 1
        time.sleep(0.5)
    test_d["stop %d" % process_num] = datetime.datetime.now()


if __name__ == '__main__':
    ## define the dictionary to be used to communicate
    manager = Manager()
    test_d = manager.dict()

    ## start 5 processes
    process_list=[]
    for ctr in range(5):
        p = Process(target=test_f, args=(ctr, random.randint(5, 15), test_d))
        p.start()
        process_list.append(p)

    alive=True
    while alive:     ## wait for all processes to finish
        alive=False
        for p in process_list:
            if p.is_alive():
                alive=True

    for ctr in range(5):
        process_list[ctr].join()
        print("process number", ctr, test_d["stop %d"  % ctr] -
              test_d["start %d" % ctr])
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Timing actions with Python dangermaus33 0 1,002 Apr-19-2022, 10:08 PM
Last Post: dangermaus33
  Inconsistent counting / timing with threading rantwhy 1 1,755 Nov-24-2021, 04:04 AM
Last Post: deanhystad
  Synchronization/Timing Problem quest 5 2,959 Mar-31-2021, 10:26 PM
Last Post: quest
  Timing of a while loop stylingpat 4 6,781 Mar-31-2021, 10:48 AM
Last Post: stylingpat
  Assigning Data from one column to another with different associated timing interval alexafshari 1 1,944 Apr-30-2020, 03:59 PM
Last Post: pyzyx3qwerty
  Frequency and timing of psycopg2 commits acecase 0 2,059 Nov-01-2019, 05:50 PM
Last Post: acecase
  Perpetual timing Mark17 3 2,862 Oct-24-2019, 03:46 PM
Last Post: Gribouillis
  Timing input Mark17 2 2,271 Oct-23-2019, 08:25 PM
Last Post: Mark17
  GPIO output timing help needed skid 5 4,046 Jan-23-2018, 04:12 PM
Last Post: skid
  change timing on py script kwfreverie 2 3,105 Dec-16-2017, 07:35 PM
Last Post: kwfreverie

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020