有没有办法在python3中故意使数据损坏?

时间:2014-10-24 08:50:55

标签: python python-2.7 python-3.x

我现在正在制作一个基本上破坏数据的应用程序。这就是它的作用。但是,我找不到将损坏的数据保存到变量中的方法。我想将损坏的数据保存在python列表中,例如" holder = []"以后可以访问它们。有没有办法做到这一点?

import random
import time
import threading


def example():
    for counter in range(10):
        print(counter)


def thread_set_1():
    thread1=threading.Thread(target=example)
    thread2=threading.Thread(target=example)
    thread3=threading.Thread(target=example)
    thread4=threading.Thread(target=example)
    thread5=threading.Thread(target=example)
    thread6=threading.Thread(target=example)
    thread7=threading.Thread(target=example)
    thread8=threading.Thread(target=example)
    thread1.start()
    thread2.start()
    thread3.start()
    thread4.start()
    thread5.start()
    thread6.start()
    thread7.start()
    thread8.start()

def thread_set_2():
    thread1=threading.Thread(target=thread_set_1)
    thread2=threading.Thread(target=thread_set_1)
    thread3=threading.Thread(target=thread_set_1)
    thread4=threading.Thread(target=thread_set_1)
    thread5=threading.Thread(target=thread_set_1)
    thread6=threading.Thread(target=thread_set_1)
    thread7=threading.Thread(target=thread_set_1)
    thread8=threading.Thread(target=thread_set_1)
    thread1.start()
    thread2.start()
    thread3.start()
    thread4.start()
    thread5.start()
    thread6.start()
    thread7.start()
    thread8.start()

1 个答案:

答案 0 :(得分:0)

如果我理解正确,您想保存输出?我认为这不可能使用简单的数组,因为全局解释器锁将阻止多个线程同时访问全局变量。但是,如果将输出写入文件,则可以使用。如果您更改功能example,请执行以下操作:

def example():
    for counter in range(10):
        with open('testfile.txt','a') as fid:
            fid.write(str(counter))

然后运行:

open('testfile.txt','w') #create empty file
thread_set_1()
thread_set_2()

testfile.txt将包含看似随机的数字。但是,这将非常慢,因为每次写入数字时都会打开和关闭文件。

不使用此过程创建随机数的主要原因是它们不会真正随机,因为写入文件的数字会随着时间的推移而增加。你写道,你计划事后洗牌;如果您计划使用random(这是您的导入),为什么还要另外创建自己的随机数生成器?

相关问题