原理
进程之间,编程语言可以不同,内存不共享。需要用socket/IRC/RPC等技术实现进程间通信
线程之间,编程语言必须相同,内存共享。类变量self.xxx
共享,分叉target的函数内变量private
不共享。
代码
#! /usr/bin/env -S conda run --live-stream -n base python
import threading as th
import time
gap_sec = 0.1
max_lines = 300
class MyThread:
def __init__(self, public):
self.count = 0
self.pub = public
self.threads = []
def fork(self, private):
pid = th.get_ident()
self.threads.append(pid)
pidx = self.threads.index(pid)
while self.count < max_lines:
# report first
print(f'({self.count})\t{pidx}.\tpri={private}\tpub={self.pub} ')
self.count += 1
# then fork
t = th.Thread(target=self.fork, args=(private+1,))
time.sleep(gap_sec*0.7*private) # 用质数,抢占打印概率减小
try:
t.start()
except RuntimeError:
break
self.threads.append(t.ident)
print(f'({self.count})\tfork: {pidx}. > {self.threads.index(t.ident)}. ') # {self.threads}
self.count += 1
self.pub += 1
time.sleep(gap_sec*1.1)
thread = MyThread(0)
thread.fork(0)
运行结果
(0) 0. pri=0 pub=0
(1) fork: 0. > 1.
(2) 1. pri=1 pub=1
(3) fork: 1. > 3.
(4) 3. pri=2 pub=2
(5) 0. pri=0 pub=2
(6) fork: 0. > 5.
(7) 5. pri=1 pub=3
(8) 1. pri=1 pub=3
(9) fork: 5. > 7.
(10) 7. pri=2 pub=4
(11) fork: 3. > 9. (11) 9. pri=3 pub=4
(13) 0. pri=0 pub=5
(14) fork: 0. > 11.
(15) 11. pri=1 pub=6
(16) fork: 1. > 13.
(17) 13. pri=2 pub=7
(18) 5. pri=1 pub=7
(19) fork: 11. > 15.
(19) 15. pri=2 pub=7
(21) 3. pri=2 pub=8
(22) fork: 7. > 17.
(23) 17. pri=3 pub=9
(24) 0. pri=0 pub=9
(25) 19. pri=1 pub=9 (25) fork: 0. > 19.
(27) 1. pri=1 pub=10
(28) fork: 5. > 21.
(29) 21. pri=2 pub=11
(30) fork: 13. > 23. (30) 23. pri=3 pub=11
(32) 11. pri=1 pub=12
(33) 25. pri=2 pub=12
(33) fork: 19. > 25.
(35) fork: 9. > 27. (35) 27. pri=4 pub=13
(37) 7. pri=2 pub=14
(37) fork: 1. > 29.
(39) 29. pri=2 pub=15
(40) fork: 15. > 31.
(41) 31. pri=3 pub=16
(42) 0. pri=0 pub=16
(43) fork: 0. > 33.
(44) 33. pri=1 pub=17
(45) 35. pri=3 pub=17 (45) fork: 3. > 35.
(47) 5. pri=1 pub=18
(48) 37. pri=2 pub=18
(49) fork: 11. > 37.
(50) 13. pri=2 pub=19
(51) fork: 21. > 39.
(52) 39. pri=3 pub=20
(53) 19. pri=1 pub=20
(54) fork: 33. > 41.
(55) 41. pri=2 pub=21
(56) 9. pri=3 pub=21
(57) fork: 17. > 43.
(57) 43. pri=4 pub=21
(59) 1. pri=1 pub=22
(60) 45. pri=3 pub=22
(61) 46. pri=2 pub=22
(62) 15. pri=2 pub=22
(62) fork: 5. > 46.
(64) fork: 25. > 45.
(65) 0. pri=0 pub=24
(66) fork: 0. > 49.
(67) 49. pri=1 pub=25
(68) 3. pri=2 pub=25
(69) fork: 7. > 51.
(70) fork: 29. > 52.
(71) 52. pri=3 pub=27
(72) 51. pri=3 pub=27
(73) 11. pri=1 pub=27 (73) 55. pri=2 pub=27
(75) fork: 19. > 55.
(76) fork: 23. > 57.
(77) 57. pri=4 pub=29
(78) 21. pri=2 pub=29
(79) fork: 37. > 59.
(80) 59. pri=3 pub=30
(81) fork: 1. > 61.
(82) 61. pri=2 pub=31
(83) 33. pri=1 pub=31
(84) fork: 49. > 63. (84) 63. pri=2 pub=31
(86) fork: 13. > 65. (86) 17. pri=3 pub=32
(88) 65. pri=3 pub=33
(89) fork: 31. > 67.
(89) 67. pri=4 pub=33
(91) 25. pri=2 pub=34 (91) 5. pri=1 pub=34
(93) fork: 41. > 69.
(94) 70. pri=2 pub=35
(95) fork: 11. > 70.
(96) 69. pri=3 pub=36
(97) 0. pri=0 pub=36
(98) fork: 0. > 73.
(99) 73. pri=1 pub=37
(100) fork: 35. > 75.
(101) 75. pri=4 pub=38
(102) 7. pri=2 pub=38
(103) 29. pri=2 pub=38
(104) fork: 15. > 77.
(104) 77. pri=3 pub=38
(106) fork: 46. > 79.
(107) 79. pri=3 pub=40
(108) 19. pri=1 pub=40
(109) fork: 33. > 81.
(110) 81. pri=2 pub=41
(111) fork: 27. > 83.
(112) 83. pri=5 pub=42
(113) 23. pri=3 pub=42 (113) fork: 3. > 85.
(114) 85. pri=3 pub=43
(116) fork: 39. > 87.
(117) 87. pri=4 pub=44
(118) 37. pri=2 pub=44 (118) fork: 55. > 89.
(120) 89. pri=3 pub=45
(121) 1. pri=1 pub=45
(122) fork: 5. > 91.
(122) 91. pri=2 pub=45
(124) fork: 73. > 93.
(125) 93. pri=2 pub=47
(126) 49. pri=1 pub=47
(127) 95. pri=4 pub=47 (127) fork: 9. > 95.
(129) 13. pri=2 pub=48
(130) 31. pri=3 pub=48
(130) 97. pri=4 pub=48
(131) fork: 45. > 97.
(133) fork: 21. > 99.
(134) 99. pri=3 pub=50
(134) fork: 61. > 101.
(136) 101. pri=3 pub=51
(137) 103. pri=2 pub=51 (137) fork: 19. > 103.
(139) 41. pri=2 pub=52
(140) 11. pri=1 pub=52
(141) 105. pri=3 pub=52
(141) fork: 63. > 105.
(143) 0. pri=0 pub=53
(144) 107. pri=1 pub=53
(145) fork: 0. > 107.
(146) 35. pri=3 pub=54
(147) 109. pri=4 pub=54 (147) 110. pri=4 pub=54
(149) fork: 52. > 110.
(149) fork: 51. > 109.
(151) 15. pri=2 pub=56 (151) 113. pri=2 pub=56
(152) fork: 1. > 113.
(154) 46. pri=2 pub=57
(155) 115. pri=3 pub=57
(156) 116. pri=3 pub=57
(157) fork: 70. > 115.
(158) fork: 25. > 116.
(159) 33. pri=1 pub=59
(160) fork: 49. > 119.
(160) 119. pri=2 pub=59
(162) 27. pri=4 pub=60
(163) fork: 43. > 121.
(164) 121. pri=5 pub=61
(165) 3. pri=2 pub=61
(166) 39. pri=3 pub=61
(167) 123. pri=4 pub=61
(167) fork: 59. > 123.
(169) fork: 7. > 125.
(170) 125. pri=3 pub=63
(171) fork: 29. > 127.
(172) 127. pri=3 pub=64
(173) 55. pri=2 pub=64
(174) 5. pri=1 pub=64
(175) fork: 11. > 129.
(176) 129. pri=2 pub=65
(177) fork: 81. > 131.
(177) 131. pri=3 pub=65
(179) 73. pri=1 pub=66
(180) fork: 107. > 133.
(181) 133. pri=2 pub=67
(182) 9. pri=3 pub=67
(183) 135. pri=4 pub=67
(184) fork: 65. > 135.
(185) 137. pri=4 pub=68
(186) fork: 17. > 137.
(187) 45. pri=3 pub=69
(188) fork: 37. > 139.
(189) 21. pri=2 pub=70
(189) 61. pri=2 pub=70
(190) 139. pri=3 pub=70
(191) 141. pri=3 pub=70
(192) fork: 91. > 141.
(193) 143. pri=4 pub=71
(194) fork: 69. > 143.
(196) 19. pri=1 pub=72
(197) 63. pri=2 pub=72
(198) fork: 33. > 145.
(198) fork: 93. > 146.
(198) 145. pri=2 pub=72 (200) 146. pri=3 pub=74
(202) 149. pri=5 pub=74
(203) fork: 57. > 149.
(204) 0. pri=0 pub=75
(205) fork: 0. > 151.
(206) 151. pri=1 pub=76
(207) fork: 13. > 153.
(208) 153. pri=3 pub=77
(208) 51. pri=3 pub=77 (208) 52. pri=3 pub=77
(209) 155. pri=4 pub=77 (211) fork: 77. > 157.
(211) fork: 79. > 155.
(214) 157. pri=4 pub=79
(215) 25. pri=2 pub=79
(215) 1. pri=1 pub=79 (216) 159. pri=3 pub=79
(217) 70. pri=2 pub=79
(217) fork: 103. > 159.
(218) 161. pri=3 pub=79
(220) fork: 41. > 161.
(222) 163. pri=2 pub=81
(223) fork: 5. > 163.
(224) fork: 73. > 165.
(225) 165. pri=2 pub=83
(226) 49. pri=1 pub=83
(227) 43. pri=4 pub=83
(228) 167. pri=4 pub=83
(229) 168. pri=5 pub=83
(229) fork: 23. > 167.
(230) fork: 67. > 168.
(231) fork: 85. > 171.
(232) 171. pri=4 pub=85
(234) fork: 89. > 173. (234) 59. pri=3 pub=86 (234) 173. pri=4 pub=86
(235) 7. pri=2 pub=86
(238) 29. pri=2 pub=87
(239) fork: 113. > 175.
(240) 175. pri=3 pub=88
(241) fork: 15. > 177.
(242) 177. pri=3 pub=89
(243) fork: 46. > 179.
(244) 179. pri=3 pub=90
(245) fork: 19. > 181. (245) 11. pri=1 pub=90
(247) 181. pri=2 pub=91
(248) 81. pri=2 pub=91
(249) 183. pri=3 pub=91
(249) fork: 119. > 183.
(251) 185. pri=5 pub=92 (251) fork: 75. > 185.
(253) 107. pri=1 pub=93
(254) 187. pri=2 pub=93
(255) fork: 151. > 187.
(256) fork: 3. > 189. (256) 189. pri=3 pub=94
(258) 65. pri=3 pub=95
(259) 17. pri=3 pub=95
(260) fork: 31. > 191.
(261) 191. pri=4 pub=96
(262) 193. pri=4 pub=96
(263) fork: 99. > 194.
(263) fork: 101. > 193.
(264) 194. pri=4 pub=97
(266) fork: 55. > 197. (266) 197. pri=3 pub=98
(268) 37. pri=2 pub=99
(269) 69. pri=3 pub=99
(270) 91. pri=2 pub=99
(271) 199. pri=3 pub=99 (271) fork: 129. > 199.
(273) fork: 105. > 201.
(274) 201. pri=4 pub=101
(275) fork: 1. > 203.
(276) 203. pri=2 pub=102
(277) fork: 133. > 205.
(277) 205. pri=3 pub=102
(279) 33. pri=1 pub=103
(279) 93. pri=2 pub=103
(281) fork: 49. > 207.
(281) 207. pri=2 pub=103
(283) 57. pri=4 pub=104 (283) fork: 35. > 209.
(285) 209. pri=4 pub=105
(286) fork: 87. > 211.
(287) 211. pri=5 pub=106
(288) 0. pri=0 pub=106
(289) fork: 0. > 213.
(290) 213. pri=1 pub=107
(291) 13. pri=2 pub=107
(292) fork: 61. > 215.
(293) 215. pri=3 pub=108
(294) 77. pri=3 pub=108
(295) 79. pri=3 pub=108 (295) 217. pri=4 pub=108
(295) fork: 115. > 218.
(296) 218. pri=4 pub=109
(299) fork: 116. > 217.
(300) fork: 21. > 221.
(301) fork: 11. > 221.
(302) fork: 63. > 221.
(303) fork: 145. > 221.
(304) fork: 95. > 5.
(305) fork: 107. > 5.
(306) fork: 39. > 73.
(307) fork: 97. > 41.
(308) fork: 125. > 23.
(309) fork: 127. > 67.
(310) fork: 131. > 113.
(311) fork: 25. > 15.
(312) fork: 70. > 15.
(313) fork: 163. > 15.
(314) fork: 83. > 15.
(315) fork: 165. > 19.
(316) fork: 33. > 15.
(317) fork: 9. > 15.
(318) fork: 109. > 19.
(319) fork: 110. > 75.
(320) fork: 213. > 151.
(321) fork: 45. > 3.
(322) fork: 29. > 75.
(323) fork: 7. > 31.
(324) fork: 139. > 19.
(325) fork: 141. > 75.
(326) fork: 181. > 55.
(327) fork: 81. > 75.
(328) fork: 146. > 75.
(329) fork: 27. > 1.
(330) fork: 187. > 133.
(331) fork: 123. > 87.
(332) fork: 153. > 87.
(333) fork: 51. > 49.
(334) fork: 52. > 35.