Python2.7帮助线程和无限循环

2024-10-01 15:46:55 发布

您现在位置:Python中文网/ 问答频道 /正文

首先,我对编程非常陌生,所以提前向您道歉:p

我在做一个噩梦,试图让一个无限循环保持无限。。。你知道吗

循环(在一个单独的线程上运行)在一段随机的时间内完全按照预期运行,然后停止,没有错误。你知道吗

它可能会运行5分钟或5小时,然后就停下来。。。 我的程序不冻结或崩溃,它只是字面上的循环停止。你知道吗

拜托,这方面的任何帮助都将是惊人的,这简直是要杀了我。你知道吗

# TextWars
# RegenThread.py

# This module will constantly regenerate the players health and magika over time

# Py Lib
import threading, time, datetime

# textwars sub-pckg
import db.DBFuncs as db

# textwars main
import AppFuncs
import StatsBar


def regen():
  next_call = time.time()
  player = AppFuncs.current_player

  while 1:
    # print exact date and time for admin use to ensure the thread
    # is not drifting. Comment out when not in use
    print datetime.datetime.now()

    # Get current player stats from db
    stats = db.getRow(player)
    time.sleep(0.25)
    # A var to be used to ensure player health never exceeds their max health.
    # max_health minus regen amount
    almost_health = stats[11] - 0.07


    # If player isn't in a battle, run the regen
    if AppFuncs.in_event == False:
      time.sleep(0.25)
      if stats[4] < almost_health:
        stats[4] += 0.07
      elif stats[4] >= almost_health:
        stats[4] = stats[11]

      time.sleep(0.25)

      # Save game if stats were changed
      db.saveGame(stats[0], stats[3], stats[4], stats[5], stats[6], stats[7], stats[8],
                    stats[9], stats[10], stats[11], stats[12], stats[13], stats[14])
      time.sleep(0.25)
      # Update stats bar    
      StatsBar.StatsBar()

    # Wait 2 secs
    next_call = next_call + 2
    time.sleep(next_call - time.time())


# Call the thread from another module
def call():    
  thread = threading.Thread(target=regen)
  thread.setDaemon(True)
  thread.start()

Tags: theimportdbdatetimetimestatssleepcall

热门问题