0

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

4

1 に答える 1

4

モジュールに再バインドtimeしています:

from time import time, sleep

import time

time2 行目は、最初にインポートした行を置き換えます。このモジュールから 1 つのスタイルのインポートを選択し、それに固執してください。

import time

s = scheduler(time.time, time.sleep)

# ...
    pubtime = time.strftime('%H:%M')

または使用

from time import time, sleep, strftime

s = scheduler(time, sleep)

# ...
    pubtime = strftime('%H:%M')
于 2013-06-17T23:05:34.837 に答える