Python Forum
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()
Reply
#2
Seems like a job for "place holders"

first_variable{} = "p{}".format(number_you_want)
Reply
#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()
Reply
#4
or just
processes[-1].start() # the last line from @stullis code
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#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.
Reply
#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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  loop for dynamic cut string - cleaner way? korenron 4 1,877 Nov-22-2021, 02:30 PM
Last Post: korenron
  Optmized way to rewrite this very slow code liva28 0 1,472 Jul-18-2021, 12:16 PM
Last Post: liva28
  How do I rewrite this .bat file code onto Python? SteampunkMaverick12 4 2,787 Nov-02-2019, 11:28 PM
Last Post: snippsat
  Rewrite a function to make it work with 'bottle-pymysql' nikos 1 1,935 Feb-26-2019, 02:59 PM
Last Post: nikos
  piexif rewrite exif data yawstick 2 3,272 Oct-09-2018, 08:56 PM
Last Post: yawstick
  Not having to rewrite 'obj.' on each line beuaaa 4 3,098 Sep-30-2018, 02:21 AM
Last Post: micseydel
  How to rewrite image file name based on ocr data.txt kevinchr 0 3,623 Apr-16-2018, 07:09 PM
Last Post: kevinchr
  how do i rewrite this code to give me 10 outputs BlackPimpernel 2 2,629 Mar-29-2018, 11:29 AM
Last Post: BlackPimpernel
  Rewrite variable every n times andrea0913 0 2,573 Aug-20-2017, 05:54 PM
Last Post: andrea0913

Forum Jump:

User Panel Messages

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