• 大小: 897B
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2021-05-29
  • 语言: Python
  • 标签: Python  多线程  

资源简介

Python的多线程示例程序。zip压缩包里只有一个run.py文件,演示怎么在python里使用多线程。

资源截图

代码片段和文件信息

#_*_ coding:utf-8 _*_
import requests
import json
import time
import re
import thread
import threading
haveRun = False
printlock = threading.Lock()
def mprint(mstr):
    printlock.acquire()
    print(mstr.encode(‘gb2312‘))
    printlock.release()
def ThreadFun(datauserpassword):
    mprint(user+‘--进入线程:‘+data+‘;‘+password)
    while(True):
        time.sleep(5)
        try:
            succon = AddGRRZ(data user password)
            # mprint(con)
            if suc:
                mprint(user+‘退出线程:添加成功‘)
                break
            else:
                mprint(user+‘添加失败,重试‘)
                continue
        except:
            mprint(user+‘日志服务器异常,重试‘)
            continue

hatime = False
def schedule():
    while(True):
        try:
            timenow = time.strftime(‘%H:%M‘ time.localtime(time.time()))
            if timenow.find(‘21:55‘) > -1:
                if not haveRun:
                    data = time.strftime(‘%Y-%m-%d‘ time.localtime(time.time()))
                    mprint(‘‘+data)
                    thread.start_new_thread(ThreadFun (data‘user‘‘123456‘))
                    thread.start_new_thread(ThreadFun (data ‘user2‘ ‘123456‘))
                    thread.start_new_thread(ThreadFun (data ‘user3‘ ‘4‘))
                    thread.start_new_thread(ThreadFun (data ‘user4‘ ‘sss‘))
                    haveRun = True
            else:
                haveRun=False
            if timenow.find(‘:44‘)>-1:
                if not hatime:
                    mprint(timenow+‘--running...‘)
                    hatime=True
            else:
                hatime=False
        except Exception as e:
            continue
        time.sleep(1)

def main():
    mprint(‘开始运行‘)
    schedule()


def AddGRRZ(datauserNamepassWord):
    #define url
    mprint(userName+‘;AddGRRZ();‘+data+‘;pw:‘+passWord)
    

if __name__ == “__main__“:
    import sys
    reload(sys)
    sys.setdefaultencoding(‘utf8‘)
    main()

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     文件        2109  2019-02-26 22:03  run.py

评论

共有 条评论