在heroku上运行单个dyno时,threading
模块是否有效?
例如:
import threading
import time
import random
def foo(x, s):
time.sleep(s)
print ("%s %s %s" % (threading.current_thread(), x, s))
for x in range(4):
threading.Thread(target=foo, args=(x, random.random())).start()
应该返回类似......
的内容$ python3 mythread.py
<Thread(Thread-3, started 123145318068224)> 2 0.27166873449907303
<Thread(Thread-4, started 123145323323392)> 3 0.5510182055055494
<Thread(Thread-1, started 123145307557888)> 0 0.642366815814484
<Thread(Thread-2, started 123145312813056)> 1 0.8985126103340428
是吗?
答案 0 :(得分:9)
是。这工作正常=)刚刚测试了最新的Python 3版本。您可以自己在Heroku上轻松测试。
Heroku dynos正在使用虚拟CPU内核,但线程仍然正常。
编辑:这是我的Heroku日志
2016-08-02T20:18:35.040230+00:00 heroku[test.1]: State changed from starting to up
2016-08-02T20:18:36.871061+00:00 app[test.1]: <Thread(Thread-3, started 140472762279680)> 2 0.10491314677740204
2016-08-02T20:18:36.969173+00:00 app[test.1]: <Thread(Thread-1, started 140472795842304)> 0 0.2034461123977389
2016-08-02T20:18:37.117934+00:00 app[test.1]: <Thread(Thread-2, started 140472779060992)> 1 0.35186381754517004
2016-08-02T20:18:37.476239+00:00 app[test.1]: <Thread(Thread-4, started 140472542557952)> 3 0.7093646481085698