I am trying to run two python files (A.py and B.py) at the same time. I tried to run them in two different and use two separate commands to run them, it works well.
The problem is, I hope to have a script file to run them in parallel. I tried multi-processing in as the following code:
if __name__ == '__main__':
jobs=[]
jobs.append(Process(target=A.start))
jobs.append(Process(target=B.start))
for job in jobs:
job.start()
for job in jobs:
job.join()
The result is it runs A and B twice, which I hope them to run only once for each of them.
What is the cause for the problem and how can I solve it? Or is there any other solution that I can run two python files in parallel?
Thanks for your help in advance.
For my import information, I have three files: A.py, B.py and run.py.
In A.py, I have:
from scapy.all import *
from scapy.layers.http import HTTPRequest
from scapy.layers.http import HTTPResponse
from colorama import init, Fore
import docker
import time
import redis
In B.py, I have:
import json
import docker
import socket
import time
import psutil
import socket
import redis
import prometheus_client
from prometheus_client import Gauge,Counter
from prometheus_client.core import CollectorRegistry
from flask import Response, Flask
And in the run.py, I have:
from multiprocessing import Process
import A
import B
====UPDATE PROBLEM CAUSE====
After hours of playing with the code, I think I found the cause, but I do not understand why.
The cause is in my A.py file, I have a code
#flask starts
app.run(host="0.0.0.0", port=15600,debug=True)
If I removed the debug mode, the code works well. The problem only occurs when the debug mode is on.
Does anyone know why it happens?