Python 2.7帮助 - 线程和无限循环

时间:2015-07-09 17:39:17

标签: python multithreading while-loop

首先,我对编程非常陌生,所以提前抱歉: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()

0 个答案:

没有答案
相关问题