正确启动新线程,在python中启动新进程

时间:2021-11-29 02:29:58

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.

我有一个程序(proc1),在它的工作中间我创建一个新线程(Thr1),我开始一个新的进程(Proc2)。 Thr1 - 有一个无限循环等待来自Proc2的信号。 Proc2是一个始终等待来自任何客户端的连接的服务器。

in proc1 I start a new thread like this:

在proc1中我开始这样一个新线程:

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)>]

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

From the Thr1 I have next output:

从Thr1我有下一个输出:

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

线程[<_MainThread(MainThread,started 140134162933632)>,Thread(Thread-1,140133872453376)>]中的THREAD

in the Thr1 I start proc2 like this:

在Thr1我开始proc2像这样:

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

And in proc2 I have the next output:

在proc2中我有下一个输出:

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

proc中的线程[Thread(Thread-1,started 140133872453376)>]

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

新proc2与Thr1具有相同的ID是正常的吗?

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.

我问,因为这两个程序proc1和proc2分别工作正常。但我需要将它们组合起来,proc1应该启动proc2。当我将它们组合在一起时,有时候我开始出现Segmentation故障。每次运行都不会出现故障。只是有时。有时服务器接收数据然后proc1失败,有时Thr1启动然后程序失败。在Thr1和proc2开始后它失败了,我想我做错了什么。

How to start process in thread correctly?

如何正确启动线程进程?

1 个解决方案

#1


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.

threading.enumerate返回所有活动线程的列表。你的proc2不是一个主题。这是一个过程。我认为这是不同的东西。在proc2中调用enumerate的那一刻,只有从proc1生成的Thread1才有效。由于Thread1具有GIL,因此proc1中的MainThread被暂停。

#1


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.

threading.enumerate返回所有活动线程的列表。你的proc2不是一个主题。这是一个过程。我认为这是不同的东西。在proc2中调用enumerate的那一刻,只有从proc1生成的Thread1才有效。由于Thread1具有GIL,因此proc1中的MainThread被暂停。