当我运行时,它告诉我:NameError:未定义名称“锁"? [英] When I run it tells me this : NameError: name 'lock' is not defined?

查看:172
本文介绍了当我运行时,它告诉我:NameError:未定义名称“锁"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

•假定您有一个包含500,000个元素的数组(data = []),并且为每个元素分配了1到10之间的随机值(random.randint(1,10)).

• 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)) .

对于范围在(500000)中的i: 数据[i] = random.randint()

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

•允许用户确定辅助线程数(N).用户可以输入1到10之间的值.无效的值将导致警告消息,并且线程数设置为5. •制定一种将阵列自动划分为N个相等段的方法,其中N是工作线程数(threadCount).您不能创建子数组来处理此问题;您必须设计出一种基于索引对原始数组进行分区的方法.假设用户输入的threadCount为4,则会在下面提供提示.

• 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.

第1段 (125,000个元素)第2段 (125,000个元素)第3段 (125,000个元素)第4段 (125,000个元素)

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 sumsum(st,end,threadIndex):

def summation(st, end, threadIndex):

其中:st和end代表数组段的起点和终点,而index是线程号.

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, locks
    for 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)/wt

st=0

for i in range(wt):
    st= i * seg
    end = st *seg -1
    thread.start_new_thread(su, ())
    locks = lock.acquire()
avg = sum(subtotal)/len(data)

print(avg)

推荐答案

您丢失了

lock = threading.Lock()

我认为您应该导入threading而不是thread:

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.

这篇关于当我运行时,它告诉我:NameError:未定义名称“锁"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆