简体   繁体   中英

Correct start of new thread which starts new process in python

I have a program (proc1) where in the middle of it's work I create a new thread (Thr1) where I start a new process (Proc2). Thr1 - has an infinite loop an waiting for a signal from Proc2. Proc2 is a server which always waits for a connection from any client.

in proc1 I start a new thread like this:

client = Thread(target=func , kwargs=some)
client.start()
print 'THREADs', threading.enumerate()

And the output is:

THREADs [<_MainThread(MainThread, started 140134162933632)>, Thread(Thread-1, started 140133872453376)>]

From the Thr1 I have next output:

THREADs in thread [<_MainThread(MainThread, started 140134162933632)>, Thread(Thread-1, started 140133872453376)>]

in the Thr1 I start proc2 like this:

p = Process(target=t, args=(receive, ))
p.start()

And in proc2 I have the next output:

THREADs in proc [Thread(Thread-1, started 140133872453376)>]

Is it normal that new proc2 has the same id as the Thr1?

I am asking because these two programs proc1 and proc2 work fine separately. But I need to combine them so, that proc1 should starts proc2. And when I combined them, sometimes I started to get Segmentation fault. Fault appears not at every running. Only sometimes. Sometimes server receives data and then proc1 fails, sometimes Thr1 starts and then program fails. It fails after Thr1 and proc2 started and I think I do something wrong.

How to start process in thread correctly?

You mix threads and processes intentionally? Why not everything as new process?

threading.enumerate returns a list of all active threads . Your proc2 is not a thread. It is a process. I think that it is something different. The moment you call enumerate in your proc2, only Thread1 spawned from your proc1 is active. The MainThread in proc1 is suspended as the Thread1 has the GIL.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM