Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Cleaner way to rewrite
#1
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()
Quote
#2
Seems like a job for "place holders"

first_variable{} = "p{}".format(number_you_want)
Quote
#3
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()
micseydel and buran like this post
Quote
#4
or just
processes[-1].start() # the last line from @stullis code
stullis and micseydel like this post
Quote
#5
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.
Quote
#6
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.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  How do I rewrite this .bat file code onto Python? SteampunkMaverick12 4 170 Nov-02-2019, 11:28 PM
Last Post: snippsat
  Rewrite a function to make it work with 'bottle-pymysql' nikos 1 286 Feb-26-2019, 02:59 PM
Last Post: nikos
  piexif rewrite exif data yawstick 2 715 Oct-09-2018, 08:56 PM
Last Post: yawstick
  Not having to rewrite 'obj.' on each line beuaaa 4 646 Sep-30-2018, 02:21 AM
Last Post: micseydel
  How to rewrite image file name based on ocr data.txt kevinchr 0 967 Apr-16-2018, 07:09 PM
Last Post: kevinchr
  how do i rewrite this code to give me 10 outputs BlackPimpernel 2 654 Mar-29-2018, 11:29 AM
Last Post: BlackPimpernel
  Rewrite variable every n times andrea0913 0 888 Aug-20-2017, 05:54 PM
Last Post: andrea0913

Forum Jump:


Users browsing this thread: 1 Guest(s)