请问一下怎么能让下面的 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()
1
HypoChen 2018-04-24 10:47:51 +08:00 1
信号量了解一下?
|
3
qieqie 2018-04-24 11:08:09 +08:00 1
from multiprocessing import Process, Lock
import time lock = Lock() class ScheduleTest(): @staticmethod 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() |
4
qieqie 2018-04-24 11:11:36 +08:00
忘了写成 code block 了 - -
|
5
aice114 OP @qieqie 谢谢了啊,不过我那个需要 my_progress 在多进程的情况下只运行一次,也就是同时只有一个 printx 在运行,加锁的方式还是不行,3 个进程同时都在打印了
|
6
SWBMESSI 2018-04-24 11:28:34 +08:00 via iPhone
信号量.....操作系统了解一下?
|
8
ai277014717 2018-04-24 11:59:40 +08:00
python 有全局变量吗?写个 get 和 set 来控制全局变量,get set 加锁。
my_process 之前 get 全局变量大于 1 就 return |
9
symons 2018-04-24 13:58:00 +08:00
借助 mysql 或者 mongodb 啥的来控制吗
|
10
windardyang 2018-04-24 15:36:21 +08:00 1
#### 进程锁
进程锁的位置很重要 ``` # -*- coding: utf-8 -*- from multiprocessing import Process, Lock import time lock = Lock() 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() for i in range(3): with lock: p = Process(target=my_schedule.run) p.start() p.join() if __name__ == '__main__': app_run() ``` #### 信号量 信号量其实也是进程锁 ``` # -*- coding: utf-8 -*- from multiprocessing import Process, Semaphore import time s = Semaphore(1) class ScheduleTest(): @staticmethod def printx(): while True: print('hello x') time.sleep(5) def run(self): s.acquire() print('printx is running...') my_process = Process(target=self.printx) my_process.start() my_process.join() s.release() 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() ``` #### 共享变量 共享变量注意需加锁 ``` # -*- coding: utf-8 -*- from multiprocessing import Process, Manager, Lock import time manager = Manager() sum = manager.Value('tmp', 0) lock = Lock() class ScheduleTest(): @staticmethod def printx(): while True: print('hello x') time.sleep(5) def run(self): with lock: if not sum.value: print('printx is running...') my_process = Process(target=self.printx) my_process.start() sum.value += 1 else: print('printx has ran.') 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() ``` |
11
aice114 OP @windardyang 谢谢,灰常详细了,我仔细研究研究
|
12
woosdaf 2018-04-24 15:57:51 +08:00
厉害
|