请问一下有关 Python 多进程的问题

2018-04-24 10:46:05 +08:00
 aice114

请问一下怎么能让下面的 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()
2359 次点击
所在节点    Python
12 条回复
HypoChen
2018-04-24 10:47:51 +08:00
信号量了解一下?
aice114
2018-04-24 10:48:55 +08:00
@HypoChen 我去瞅瞅
qieqie
2018-04-24 11:08:09 +08:00
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()
qieqie
2018-04-24 11:11:36 +08:00
忘了写成 code block 了 - -
aice114
2018-04-24 11:18:46 +08:00
@qieqie 谢谢了啊,不过我那个需要 my_progress 在多进程的情况下只运行一次,也就是同时只有一个 printx 在运行,加锁的方式还是不行,3 个进程同时都在打印了
SWBMESSI
2018-04-24 11:28:34 +08:00
信号量.....操作系统了解一下?
qieqie
2018-04-24 11:53:51 +08:00
@aice114 那不是更简单了么。。在运行进程之前非阻塞的 acquire(False),失败直接 return 就行了
ai277014717
2018-04-24 11:59:40 +08:00
python 有全局变量吗?写个 get 和 set 来控制全局变量,get set 加锁。
my_process 之前 get 全局变量大于 1 就 return
symons
2018-04-24 13:58:00 +08:00
借助 mysql 或者 mongodb 啥的来控制吗
windardyang
2018-04-24 15:36:21 +08:00
#### 进程锁

进程锁的位置很重要

```
# -*- 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()

```
aice114
2018-04-24 15:49:21 +08:00
@windardyang 谢谢,灰常详细了,我仔细研究研究
woosdaf
2018-04-24 15:57:51 +08:00
厉害

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/449346

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX