Cleaner way to rewrite - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Cleaner way to rewrite (/thread-22931.html) |
Cleaner way to rewrite - fakka - Dec-03-2019 Whats a clean way to rewrite this dynamically. Rather than essentially hardcoded. ProcessInput1 is a defined function. a1,b2,c3,d4,e5,f6,g7,h8,i9,j10,k11,l12,m13,n14,o15,p16,q17,r18,s19,t20 = np.array_split(myarray,20) p1 = Process(target=processInput1, args=(a1,)) p1.start() p2 = Process(target=processInput1, args=(b2,)) p2.start() p3 = Process(target=processInput1, args=(c3,)) p3.start() p4 = Process(target=processInput1, args=(d4,)) p4.start() p5 = Process(target=processInput1, args=(e5,)) p5.start() p6 = Process(target=processInput1, args=(f6,)) p6.start() p7 = Process(target=processInput1, args=(g7,)) p7.start() p8 = Process(target=processInput1, args=(h8,)) p8.start() p9 = Process(target=processInput1, args=(i9,)) p9.start() p10 = Process(target=processInput1, args=(j10,)) p10.start() p11 = Process(target=processInput1, args=(k11,)) p11.start() p12 = Process(target=processInput1, args=(l12,)) p12.start() p13 = Process(target=processInput1, args=(m13,)) p13.start() p14 = Process(target=processInput1, args=(n14,)) p14.start() p15 = Process(target=processInput1, args=(o15,)) p15.start() p16 = Process(target=processInput1, args=(p16,)) p16.start() p17 = Process(target=processInput1, args=(q17,)) p17.start() p18 = Process(target=processInput1, args=(r18,)) p18.start() p19 = Process(target=processInput1, args=(s19,)) p19.start() p20 = Process(target=processInput1, args=(t20,)) p20.start() RE: Cleaner way to rewrite - michael1789 - Dec-03-2019 Seems like a job for "place holders" first_variable{} = "p{}".format(number_you_want) RE: Cleaner way to rewrite - stullis - Dec-03-2019 This should do it: processes = [] for x in np.array_split(myarray, 20): processes.append(Process(target=processInput, arg=(x,))) processes[len(processes) - 1].start() RE: Cleaner way to rewrite - buran - Dec-04-2019 or just processes[-1].start() # the last line from @stullis code RE: Cleaner way to rewrite - fakka - Dec-04-2019 Thats awesome stullis (and others) ... I wouldnt have have thought about it like that, adding a defined program back to array. IS that because essentially each call is being 'bound' to a variable .... like P1->P20 .... so therefore .... make sense an array is possible to wrap it in ? If that makes sense. RE: Cleaner way to rewrite - stullis - Dec-05-2019 Lists don't store values; they store references to memory locations. Anything that returns an object (and everything in Python is an object) has a memory location. So, each index in a list can be conceptualized as a variable storing a reference to some object. Leveraging that, we can do things like the code above to store anonymous objects. The same holds true for tuples, sets, and dicts as well, but they aren't necessarily as useful in this case as a list. |