When I run it tells me this : NameError: name lock is not defined?

2024/11/16 2:45:40

• Assume that you have an array (data=[]) containing 500,000 elements and that each element has been assigned a random value between 1 and 10 (random.randint(1,10)) .

for i in range (500000):data[i]=random.randint()

• Allow the users to determine the number of worker threads (N). The user can enter a value between 1 and 10. Invalid values should result in a warning message and the thread count being set to 5. • Work out a method of automatically partitioning the array into N equal segments, where N is the number of worker threads (threadCount). You cannot create subarrays to handle this problem; you must work out a method of partitioning the original array based on indices. A hint is provided below assuming that the user has entered a threadCount of 4.

Segment 1 (125,000 elements) Segment 2 (125,000 elements) Segment 3 (125,000 elements) Segment 4 (125,000 elements)

• Determine a method to spawn your threads so that each thread is assigned a segment of the array to operate on. These threads should create a sum of the elements for their allotted segment. The single threaded function prototype is given below:

def summation(st, end, threadIndex):

where: st and end represent the start and end points of the array segment, and index is the thread number.

Your must determine the locking mechanism to ensure the program can run concurrently on the array.

• After each thread has completed its work the main program should compute the final average by summing the sub-sums and diving by the total number of array elements.

Final Exercise: Can you extend your program so that the array is populated with random numbers using your worker threads? My Code:

import random
import thread
def su(st,end,i):global subtotal, data, locksfor index in range(st,end+i):subtotal[i] += data[index]lock.release()numth = int(100)data = list(range(numth))for index in range(len(data)):data[index] = random.randint(1,10)wt=int(input("enter the number of working threads:"))locks = list(range(wt))subtotal = list(range(wt))seg = len(data)/wtst=0for i in range(wt):st= i * segend = st *seg -1thread.start_new_thread(su, ())locks = lock.acquire()
avg = sum(subtotal)/len(data)print(avg)
Answer

You're missing

lock = threading.Lock()

And I think you should be importing threading instead of thread :

This module constructs higher-level threading interfaces on top of the lower level thread module.
https://en.xdnf.cn/q/119583.html

Related Q&A

Unable to find null bytes in Python code in Pycharm?

During copy/pasting code I often get null bytes in Python code. Python itself reports general error against module and doesnt specify location of null byte. IDE of my choice like PyCharm, doesnt have c…

remove single quotes in list, split string avoiding the quotes

Is it possible to split a string and to avoid the quotes(single)? I would like to remove the single quotes from a list(keep the list, strings and floats inside:l=[1,2,3,4.5]desired output:l=[1, 2, 3, …

Image Segmentation to Map Cracks

I have this problem that I have been working on recently and I have kind of reached a dead end as I am not sure why the image saved when re opened it is loaded as black image. I have (original) as my b…

operations on column length

Firstly, sorry if I have used the wrong language to explain what Im operating on, Im pretty new to python and still far from being knowledgeable about it.Im currently trying to do operations on the len…

Python: Parse one string into multiple variables?

I am pretty sure that there is a function for this, but I been searching for a while, so decided to simply ask SO instead.I am writing a Python script that parses and analyzes text messages from an inp…

How do I pull multiple values from html page using python?

Im performing some data analysis for my own knowledge from nhl spread/betting odds information. Im able to pull some information, but Not the entire data set. I want to pull the list of games and the a…

Creating h5 file for storing a dataset to train super resolution GAN

I am trying to create a h5 file for storing a dataset for training a super resolution GAN. Where each training pair would be a Low resolution and a High resolution image. The dataset will contain the d…

How to resolve wide_to_long error in pandas

I have following dataframeAnd I want to convert it into the following format:-To do so I have used the following code snippet:-df = pd.wide_to_long(df, stubnames=[manufacturing_unit_,outlet_,inventory,…

Odoo 10: enter value in Many2one field dynamically

I added in my models.py :commercial_group = fields.Many2one("simcard.simcard")and in my views.xml :<field name="commercial_group" widget="selection"/>And then i am t…

How to erode this thresholded image using OpenCV

I am trying to first remove the captcha numbers by thresholding and then eroding it ,to get slim continuous lines to get better output. Problem:the eroded image is not continuous as u can see Original …