I'm already struggeling some hours with a problem within my python project.
The situation is as follows:
I have a script A.py and a Script B.py
**Script A.py:**
#in this script the function def main() is running
def main():
#some coding in here
x=str(body)#then i assign the string of the variable body to a new variable x
#some other coding in here
if __name__=='__main__':
main()
REMIND: this a pseudo code to explain my struggle (the script as a standalone module is working properly) !
Now I have Script B.py (in the same folder)
**Script B.py** #in this script i try to run Script A.py and assign the value of variable x to a new variable in order to do furhter processing with it.
import A
A.main() # When importing the module and excuting its main() function by running B.py I see the values of variable x appearing on my screen
QUESTION: How can I assign the value of variable x now to a new variable so that i can do further processing with it in B.py ? Is this even possible ? Cause after calling the main function of A.py no other operations are processed.
Please consider that I'm a relatively newby regaring programming over several modules.
I would be very glad for any help.
Thank you very much in advance
Kind regards
Slin
Ok i tried your approaches but still not getting the desired result.
A.py is a AMQP subscribing script (https://www.rabbitmq.com/tutorials/tutorial-one-python.html) (see below):
import pika
credentials = pika.PlainCredentials('admin', 'admin')
connection = pika.BlockingConnection(pika.ConnectionParameters('Ipaddress',
5672,
'/',
credentials))
#connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
channel = connection.channel()
channel.exchange_declare(exchange='logs',
exchange_type='fanout')
result = channel.queue_declare(exclusive=True)
queue_name = result.method.queue
channel.queue_bind(exchange='logs',
queue=queue_name)
print(' [*] Waiting for logs. To exit press CTRL+C')
def callback(ch, method, properties, body):
x = str(body)
print str(x)
channel.basic_consume(callback,
queue=queue_name,
no_ack=True)
channel.start_consuming()
if __name__ == '__main__':
main()
B.py:
import pika
import A
A.main()
With the approaches so far i get the same as shown with the coding above. I would like to assign x (which values can chane when A is running) to a new variable within B.py to do some processing to publish it afterwards with the counterpart script of A.py.
When executing B.py i receive:
[*] Waiting for logs. To exit press CTRL+C
['20'] #this is the string of variable x from script A
Now i want to assign this ['20'] to a new variable wihtin B.py.. but the script B.py keeps running A.main() (which is logical cause it is a loop).
Thanks so far for your support.
Kind regards