V
V
Vyacheslav Grachunov2020-08-22 22:12:14
Python
Vyacheslav Grachunov, 2020-08-22 22:12:14

How to make a process in multiprocessing not terminate automatically?

Code like this:

def outgoing():
  BASEDIR = os.path.dirname(os.path.realpath(__file__))

  pool = PooledDB(creator=pymysql,
   
                 host='127.0.0.1',

                 user='root',

                 password='password',

                 database='tgadmin_test',

                 autocommit=True,

                 charset='utf8mb4',

                 cursorclass=pymysql.cursors.DictCursor,

                 blocking=False,

                 maxconnections=20)


  db = pool.connection()
  cursor = db.cursor()

  sql = "SELECT * FROM tgmanager_applicationusers WHERE banned_at IS NULL ORDER BY RAND() LIMIT 1"
  cursor.execute(sql)

  user1 = cursor.fetchone()
  proxy_lst = user1['proxy'].split(':')


  # VoIPServerConfig.set_bitrate_config(80000, 100000, 60000, 5000, 5000)
  client = Client(session_name=user1['phone'], api_id=user1['api_id'], api_hash=user1['api_hash'], ipv6=False, proxy=dict(hostname=str(proxy_lst[0]), port=int(proxy_lst[1]), username=str(proxy_lst[2]), password=str(proxy_lst[3])), app_version="0.22.0.1205-arm64-v8a", device_model=user1['device'], system_version=user1['os'], workdir="/home/qwentor/share/autoreg/sessions")
  client.start()


  service = VoIPNativeIOService(client, receive_calls=False)
  start_call = service.start_call('@messengerN1')
  mp3 = os.listdir(os.path.join(BASEDIR, 'speech'))
  item = random.choice(mp3)
  print('MP3##############################', item)
  speech = int(item.split('/')[-1].split(".")[0])
  print('SPEECH', speech)
  sql = """SELECT speech FROM tgmanager_dialogues WHERE id = {id}""".format(id=speech)
  cursor.execute(sql)
  res = cursor.fetchone()['speech']
  print(res)

  stream = ffmpeg.input(os.path.join(BASEDIR, 'speech', item))
  print("S1")
  stream = ffmpeg.output(stream, os.path.join(BASEDIR, 'PCM', item.split('.')[0]+'.raw'), f='s16le', ac=1, ar=48000, acodec='pcm_s16le')
  ffmpeg.run(stream)
  print("S2")
  sleep(5)
  print("S3")
  start_call.play(os.path.join(BASEDIR, 'PCM', item.split('.')[0]+'.raw'))
  print("S4")
  start_call.set_output_file(os.path.join(BASEDIR, 'outgoing.raw'))
  print("S5")


If you run it
outgoing()
somehow, everything is normal, but if
proc2 = multiprocessing.Process(target=outgoing, name='outgoing')
proc2.start()

then the program ends when print("S5") is executed, although there should be a call through tgvoip, i.e. she should wait for the phone to be lifted and chat the text, but does not wait
. What is the matter and can it be done so that the program does not end until it is necessary

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Tikhonov, 2020-09-01
@tumbler

Most likely, some of the calls inside outgoing() create new threads, so until they complete, the python interpreter does not stop, despite the fact that the main thread has finished its work.
Exactly how this interferes with multiprocessing is a matter of deep debugging, but I would first try to make exiting from outgoing() terminate the non-multiprocessing python process.

I
Ivan Yakushenko, 2020-09-01
@kshnkvn

proc2.join()
At this point, the script will hang until the process is completed.proc2

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question