python往文件里写一定格式数据、多线程开启与控制

1、往文件里写具有一定格式的数据:

    # write obj 3D face methods
    with open(obj_name, 'w') as f:
        # write vertices & colors
        for i in range(vertices.shape[1]):
            s = 'v {:.4f} {:.4f} {:.4f} {} {} {}\n'.format(vertices[1, i], vertices[0, i], vertices[2, i], colors[i, 2],
                                               colors[i, 1], colors[i, 0])
            f.write(s)

        # write f: ver ind/ uv ind
        for i in range(triangles.shape[1]):
            s = 'f {} {} {}\n'.format(triangles[0, i], triangles[1, i], triangles[2, i])
            f.write(s)

2、多线程开启与控制:

import threading
import time
class DloadThread:
    def __init__(self):
        self.exit = False

    def terminate(self, flag):
        self.exit = flag

    def setparam(self, data):
        self.params = data

    def run(self, params=None):
        self.params = params
        while not self.exit:
            str = threading.current_thread().getName()
            print(str, self.params)
        print(str, "停止")

aa = DloadThread()
bb = DloadThread()

thread = threading.Thread(target=aa.run, args=(22,), name="aa")
threadb = threading.Thread(target=bb.run, args=(25,), name="bb")
threadb.start()
thread.start()
data = input("输入改变的参数")
aa.setparam(data)
data = input("是否退出线程")
aa.terminate(True)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值