我尝试使用RabbitMQ发送消息和接收消息.我没有计算机科学背景,我使用的术语不是很准确.
我尝试复制教程文件:提交我的html表单时,我的python脚本(cgi)消息正在提交到队列
connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
channel = connection.channel()
channel.queue_declare(queue='task_queue', durable=True)
message = PN
channel.basic_publish(exchange='',
routing_key='task_queue',
body=message,
properties=pika.BasicProperties(
delivery_mode = 2, # make message persistent
))
connection.close()
Run Code Online (Sandbox Code Playgroud)
我的接收器正在运行:
connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
channel = connection.channel()
channel.queue_declare(queue='task_queue', durable=True)
print(' [*] Waiting for messages. To exit press CTRL+C')
def callback(ch, method, properties, body):
print(" [x] Received Project %r" % body)
#ch.basic_ack(delivery_tag = method.delivery_tag)
if not (os.path.isfile(js_path)):
print (' [*] ERROR files missing ')
#ch.basic_ack(delivery_tag = method.delivery_tag)
return
p= subprocess.Popen(run a subprocess here)
p.wait()
print …
Run Code Online (Sandbox Code Playgroud) 我正在调用一个子进程,我希望将子进程输出写入一个已经打开的文件。我正在使用以下代码:
f1=open('solve.out','w')
#beginning of the programm writes to this file
f_err = open('mor.err', "w")
arguments=[file.exe,arg1,arg2,...]
p=subprocess.Popen(arguments,stdout=f1, stderr=f_err)
p.wait()
f1.close()
f_err.close()
Run Code Online (Sandbox Code Playgroud)
这很好用,因为我从程序中的 .exe 获得实时输出。但是,输出都写在一行中。作为独立的,输出显示新行。
我尝试了 Universal_newlines 或 p.communicate() 没有成功。
编辑 1:windows10 Python 版本 2.7.13