Python中关于多进程的问题请教

请问一下怎么能让下面的 my_process 只运行一次呢?我试过在类里面加进程锁,但是没用,小白请教一下各位大佬

from multiprocessing import Process
import time

class ScheduleTest(): @staticmethod def printx(): while True: print(‘hello x’) time.sleep(5)

def run(self):
    print('printx is running...')
    my_process = Process(target=self.printx)
    my_process.start()

def app_run(): my_schedule = ScheduleTest() process_0 = Process(target=my_schedule.run) process_1 = Process(target=my_schedule.run) process_2 = Process(target=my_schedule.run) process_0.start() process_1.start() process_2.start()

if name == ‘main’: app_run()


Python中关于多进程的问题请教

13 回复

信号量了解一下?


我无法理解你的问题

我去瞅瞅

from multiprocessing import Process, Lock
import time

lock = Lock()

class ScheduleTest():

def printx(x):
while True:
try:
lock.acquire()
print(‘hello %d’ % x)
time.sleep(5)
except:
pass
finally:
lock.release()



def run(self, pid):
print(‘printx %d is running…’ % pid)
my_process = Process(target=self.printx, args=[pid])
my_process.start()

def app_run():
my_schedule = ScheduleTest()
process_0 = my_schedule.run(1)
process_1 = my_schedule.run(2)
process_2 = my_schedule.run(3)


if name == ‘main’:
app_run()

忘了写成 code block 了 - -

谢谢了啊,不过我那个需要 my_progress 在多进程的情况下只运行一次,也就是同时只有一个 printx 在运行,加锁的方式还是不行,3 个进程同时都在打印了

信号量…操作系统了解一下?

那不是更简单了么。。在运行进程之前非阻塞的 acquire(False),失败直接 return 就行了

python 有全局变量吗?写个 get 和 set 来控制全局变量,get set 加锁。
my_process 之前 get 全局变量大于 1 就 return

借助 mysql 或者 mongodb 啥的来控制吗

进程锁

进程锁的位置很重要

<br># -*- coding: utf-8 -*-<br>from multiprocessing import Process, Lock<br>import time<br><br><br>lock = Lock()<br><br>class ScheduleTest():<br> <br> def printx():<br> while True:<br> print('hello x')<br> time.sleep(5)<br><br> def run(self):<br> print('printx is running...')<br> my_process = Process(target=self.printx)<br> my_process.start()<br><br><br>def app_run():<br> my_schedule = ScheduleTest()<br> for i in range(3):<br> with lock:<br> p = Process(target=<a target="_blank" href="http://my_schedule.run" rel="nofollow noopener">my_schedule.run</a>)<br> p.start()<br> p.join()<br><br><br>if __name__ == '__main__':<br> app_run()<br><br>

#### 信号量

信号量其实也是进程锁

<br># -*- coding: utf-8 -*-<br>from multiprocessing import Process, Semaphore<br>import time<br><br><br>s = Semaphore(1)<br><br><br>class ScheduleTest():<br> <br> def printx():<br> while True:<br> print('hello x')<br> time.sleep(5)<br><br> def run(self):<br> s.acquire()<br> print('printx is running...')<br> my_process = Process(target=self.printx)<br> my_process.start()<br> my_process.join()<br> s.release()<br><br><br>def app_run():<br> my_schedule = ScheduleTest()<br> process_0 = Process(target=<a target="_blank" href="http://my_schedule.run" rel="nofollow noopener">my_schedule.run</a>)<br> process_1 = Process(target=<a target="_blank" href="http://my_schedule.run" rel="nofollow noopener">my_schedule.run</a>)<br> process_2 = Process(target=<a target="_blank" href="http://my_schedule.run" rel="nofollow noopener">my_schedule.run</a>)<br> process_0.start()<br> process_1.start()<br> process_2.start()<br><br><br>if __name__ == '__main__':<br> app_run()<br><br>

#### 共享变量

共享变量注意需加锁

<br># -*- coding: utf-8 -*-<br>from multiprocessing import Process, Manager, Lock<br>import time<br><br>manager = Manager()<br>sum = manager.Value('tmp', 0)<br>lock = Lock()<br><br><br>class ScheduleTest():<br> <br> def printx():<br> while True:<br> print('hello x')<br> time.sleep(5)<br><br> def run(self):<br> with lock:<br> if not sum.value:<br> print('printx is running...')<br> my_process = Process(target=self.printx)<br> my_process.start()<br> sum.value += 1<br> else:<br> print('printx has ran.')<br><br><br>def app_run():<br> my_schedule = ScheduleTest()<br> process_0 = Process(target=<a target="_blank" href="http://my_schedule.run" rel="nofollow noopener">my_schedule.run</a>)<br> process_1 = Process(target=<a target="_blank" href="http://my_schedule.run" rel="nofollow noopener">my_schedule.run</a>)<br> process_2 = Process(target=<a target="_blank" href="http://my_schedule.run" rel="nofollow noopener">my_schedule.run</a>)<br> process_0.start()<br> process_1.start()<br> process_2.start()<br><br><br>if __name__ == '__main__':<br> app_run()<br><br>

谢谢,灰常详细了,我仔细研究研究

回到顶部