我想要两个脚本在python中进行通信。我希望他们知道其他人是否失败了。
按照你在图片中看到的内容我有一些问题。当我运行rob2.py时,我会自动运行rob1.py,为什么会这样?
rob1.py
import simpy
import time
from rob2 import brok2
class Moving:
def __init__(self, env):
self.env = env
"""What does self.prov do?"""
self.prov = env.process(self.work())
self.broken = False
if self.broken == False:
global brok1
brok1 = 0
else:
brok1 = 0
#print(brok1)
def work(self):
while True:
yield env.timeout(20)
if brok2 == 1:
print("Robot 2 is not broken")
else:
print("Robot 2 is broken")
env= simpy.Environment()
moving = Moving(env)
env.run(until = 60)
rob2.py
import simpy
import time
from rob1 import brok1
class Placing:
def __init__(self, env):
self.env = env
"""What does self.prov do?"""
self.prov = env.process(self.work())
self.broken = False
if self.broken == False:
global brok2
brok2=1
else:
brok2 = 0
def work(self):
while True:
yield env.timeout(20)
time.sleep(5)
if brok1 == 1:
print("Robot 1 is not broken")
else:
print("Robot 1 is broken")
env= simpy.Environment()
placing = Placing(env)
env.run(until = 60)
当我收到尝试运行脚本的消息时,我做错了什么?
Traceback (most recent call last):
File "rob2.py", line 3, in <module>
from rob1 import brok1
File "/Users/erik/Python/python/rob1.py", line 3, in <module>
from rob2 import brok2
File "/Users/erik/Python/python/rob2.py", line 3, in <module>
from rob1 import brok1
ImportError: cannot import name 'brok1'
我发现了一些关于zeroMQ的帖子,那是去这里的方式吗?
答案 0 :(得分:0)
抛出ImportError
异常,因为Python检测到循环导入循环:在你的一个文件中导入另一个,它自己导入第一个,导入第二个,依此类推。
您需要重新组织代码以避免这种情况。 大多数(所有?)编程语言都不喜欢循环导入。