1 つのスクリプトに 2 つのコードがあり、それぞれが正常に動作しますが、両方とも同じスクリプト内で「時間」ライブラリを呼び出しているため、エラーが発生します。
"TypeError: 'module' object is not callable"
ただし、それらを個別に実行すると (もう一方をコメントアウトして)、2 つの動作するコードが生成されます。コードの例を以下に示します。 コードの各部分を機能させる方法についてのコメントに注意してください。
from sched import scheduler
from time import time, sleep
## If you comment out this line then run periodically will work fine
import time
import datetime
import random
s = scheduler(time, sleep)
random.seed()
def run_periodically(start, end, interval, func):
event_time = start
while event_time < end:
s.enterabs(event_time, 0, func, ())
event_time += interval + random.randrange(-5, 10)
s.run()
def pubdate():
return '{pubdate} {pubtime}'.format(
pubdate = datetime.date.today().strftime("%d %B %Y"),
pubtime = time.strftime('%H:%M')
)
## If you comment out this print the run periodically will work fine
print("""<pubDate>%s</pubDate>""" % (pubdate()))
def runme():
print "example prints/code"
runme()
## If you comment out this line the pubdate works fine
#run_periodically(time()+5, time()+1000000, 10, runme)
このコードを同じスクリプト内の両方の関数で動作させるには、どのような回避策が考えられるのでしょうか。よろしくAEA