Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Locks in Multithreading
#1
Hello Pythoners,

I am having trouble with some multithreading.
My goal is it to read a given list and start a thread for each element of this list, in this thread i want to sort the elements into a new empty list.

my problem is, that no matter how i set up my locks, the sorting works for the first few elements, but after that numbers, which schuld be in the middle in the list, just get appended to the end of the list

the code below for example gives me for s1 [3, 5, 7, 9, 122, 222, 213, 210]
What did i do wrong, how do i know where to put the locks ?

import threading
def parsort(v,s,lock):
    i=0
    if s==[]:
        s.append(v)
    else:
        while i < len(s):
            if s[i] > v and not v in s:
                print(v)
                s.insert(i,v)
            elif not v in s:
                s.append(v)
            i=i+1

#Mainprogramm
s1=[]
l=[5,3,7,9,122,222,213,210]
lock=threading.RLock()

for  i in l :
    sort=threading.Thread(target=parsort,args=(i,s1,lock))
    sort.start()
    for j in l:
        sort.join()


print(s1)
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Multithreading alternative MartinV279 1 176 Aug-01-2019, 11:41 PM
Last Post: scidam
  using locks in multithreading in python3 srm 2 226 Jul-13-2019, 11:35 AM
Last Post: noisefloor
  Multithreading in a loop valtih 3 7,509 Aug-03-2017, 08:20 PM
Last Post: valtih
  Multithreading with ssh connection harusin 6 2,220 Jul-10-2017, 11:40 AM
Last Post: harusin

Forum Jump:


Users browsing this thread: 1 Guest(s)