Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Queue.put()方法不使用';t创建所需的输出文件(Python)_Python_File_Queue_Output_Caesar Cipher - Fatal编程技术网

Queue.put()方法不使用';t创建所需的输出文件(Python)

Queue.put()方法不使用';t创建所需的输出文件(Python),python,file,queue,output,caesar-cipher,Python,File,Queue,Output,Caesar Cipher,我正在用python编写一个简单的caesar密码程序,使用线程和队列。即使我的程序能够运行,它也不会创建必要的输出文件。非常感谢您的帮助,谢谢 我猜异常始于我使用队列存储加密字符串的地方,如下所示: for i in range(0,len(data),l): while not q1.full: q1.put(data[index:index+l]) index+=l while not q2.empty: output_fil

我正在用python编写一个简单的caesar密码程序,使用线程和队列。即使我的程序能够运行,它也不会创建必要的输出文件。非常感谢您的帮助,谢谢

我猜异常始于我使用队列存储加密字符串的地方,如下所示:

for i in range(0,len(data),l):
    while not q1.full:
        q1.put(data[index:index+l])
        index+=l
    while not q2.empty:
        output_file.write(q2.get())
以下是全部代码:

import threading
import sys
import Queue
import string

#argumanlarin alinmasi
if len(sys.argv)!=4:
    print("Duzgun giriniz: '<filename>.py s n l'")
    sys.exit(0)
else:
    s=int(sys.argv[1])
    n=int(sys.argv[2])
    l=int(sys.argv[3])

#Global
index = 0

#kuyruk deklarasyonu
q1 = Queue.Queue(n)
q2 = Queue.Queue(2000)

lock = threading.Lock()

#Threadler
threads=[]

#dosyayi okuyarak stringe cevirme
myfile=open('metin.txt','r')
data=myfile.read()

#Thread tanimlamasi
class WorkingThread(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)

    def run(self):
        lock.acquire()
        q2.put(self.caesar(q1.get(), s))
        lock.release()

    def caesar(self, plaintext, shift):
        alphabet = string.ascii_lowercase
        shifted_alphabet = alphabet[shift:] + alphabet[:shift]
        table = string.maketrans(alphabet, shifted_alphabet)
        return plaintext.translate(table)

for i in range(0,n):
    current_thread = WorkingThread()
    current_thread.start()
    threads.append(current_thread)

output_file=open("crypted"+ "_"+ str(s)+"_"+str(n)+"_"+str(l)+".txt", "w")

for i in range(0,len(data),l):
    while not q1.full:
        q1.put(data[index:index+l])
        index+=l
    while not q2.empty:
        output_file.write(q2.get())

for i in range(0,n):
    threads[i].join()

output_file.close()
myfile.close()
导入线程
导入系统
导入队列
导入字符串
#阿古马林
如果len(sys.argv)=4:
打印(“Duzgun网格化:'.py s n l')
系统出口(0)
其他:
s=int(sys.argv[1])
n=int(sys.argv[2])
l=int(sys.argv[3])
#全球的
索引=0
#库鲁克·德克拉拉苏努
q1=队列。队列(n)
q2=队列。队列(2000)
lock=threading.lock()
#螺纹工
线程=[]
#dosyayi okuyarak stringe cevirme
myfile=open('metin.txt','r')
data=myfile.read()
#塔尼姆拉马西螺纹
类WorkingThread(threading.Thread):
定义初始化(自):
threading.Thread.\uuuuu init\uuuuuu(自)
def运行(自):
lock.acquire()
q2.put(self.caesar(q1.get(),s))
lock.release()
def caesar(自身、明文、移位):
字母表=字符串。ascii_小写
移位字母表=字母表[移位:]字母表[移位]
table=string.maketrans(字母表、移位字母表)
返回纯文本.translate(表)
对于范围(0,n)内的i:
当前线程=WorkingThread()
当前线程。开始()
附加(当前线程)
输出文件=打开(“加密的”+“\u”+str(s)+“\u”+str(n)+“\u”+str(l)+“.txt”,“w”)
对于范围内的i(0,len(数据),l):
虽然不是q1.full:
q1.put(数据[指数:指数+l])
指数+=l
虽然不是q2.empty:
output_file.write(q2.get())
对于范围(0,n)内的i:
线程[i].join()
输出_文件。关闭()
myfile.close()

而不是q1.full
永远不可能是
True
,因为
full
是一种方法,因此在布尔上下文中总是
True
,因此
不是q1.full
总是
False
,您需要调用该方法:
q1.full()
。与第二季度相同。完整

此外,在这种情况下,您不应该尝试检测队列是否已满。如果数据未满,则您将继续添加数据,直到数据满,然后忽略其余部分,或者您的
索引可以增加到
数据的大小之外,并且您将继续添加0长度的数据块

您应该使用一个单独的线程来写入
q1
和读取
q2
,然后您可以让
q1
阻塞
put()


此外,在工作线程中使用相同的锁基本上序列化所有计算,这违背了线程的目的。您要处理的问题是CPU受限的,在python中,多线程不会给您带来任何加速。查看
多处理
模块。使用(或其他一些map方法)可以大大简化整个程序,同时通过mutliprocessig提高速度。

而不是q1。full
永远不可能是
True
,因为
full
是一种方法,因此在布尔上下文中总是
True
,因此
不是q1.full
将始终是
False
,您需要调用方法:
q1.full()
。与第二季度相同。完整

此外,在这种情况下,您不应该尝试检测队列是否已满。如果数据未满,则您将继续添加数据,直到数据满,然后忽略其余部分,或者您的
索引可以增加到
数据的大小之外,并且您将继续添加0长度的数据块

您应该使用一个单独的线程来写入
q1
和读取
q2
,然后您可以让
q1
阻塞
put()


此外,在工作线程中使用相同的锁基本上序列化所有计算,这违背了线程的目的。您要处理的问题是CPU受限的,在python中,多线程不会给您带来任何加速。查看
多处理
模块。使用(或其他一些map方法)可以大大简化整个程序,同时通过mutliprocessig提高速度。

感谢您指出我的错误。我删除了试图检测队列是否已满的部分。在你的第三句话中,我不太明白我应该如何实现你刚才所说的。(顺便说一句,我的程序在删除检测条件后仍无法正常运行)。我应该打开两个线程;Writethread&Readthread?我也会尝试使用多处理来编写代码,谢谢你的建议。你只需要一个额外的线程,例如写入队列并在主线程中继续读取(或者相反)。此外,当您使用这种消费者-生产者模式时,返回结果的顺序与作业排队的顺序不同,但对于此类问题,您需要保留顺序
Pool.map()
没有这个问题,因为它保持了订单。感谢您指出我的错误。我删除了试图检测队列是否已满的部分。在你的第三句话中,我不太明白我应该如何实现你刚才所说的。(顺便说一句,我的程序在删除检测条件后仍无法正常运行)。我应该打开两个线程;Writethread&Readthread?我也会尝试使用多处理来编写代码,谢谢你的建议。你只需要一个额外的线程,例如写入队列并在主线程中继续读取(或者相反)。此外,当您使用这种消费者-生产者模式时,返回结果的顺序与作业排队的顺序不同,但对于此类问题,您需要