我正试图将一些JavaScript代码转换为Python,但JavaScript异步运行代码,并且有一个简单的事件发射器侦听器函数可以在不同的实例之间进行通信。在Python中有类似的方法吗?
我尝试过使用Process(来自多处理(,但在的任何地方都找不到简单的消息传递实现
这样的东西怎么样:
import time
import multiprocessing
def do_some_work(inputs):
# Time consuming task
time.sleep(0.1)
# Some output
return f"some response for input {inputs}"
if __name__ == "__main__":
# Create a pool of 4 workers (can be more of course)
with multiprocessing.Pool(4) as pool:
# Assign a list of work to the pool of workers and get the results
result = pool.map(do_some_work, [f"input_{i}" for i in range(100)])
# Show the results
print(result)
输出:
['some response for input input_0', 'some response for input input_1', 'some response for input input_2', 'some response for input input_3', 'some response for input input_4', 'some response for input input_5', 'some response for input input_6', 'some response for input input_7', 'some response for input input_8', 'some response for input input_9', 'some response for input input_10', 'some response for input input_11', 'some response for input input_12', 'some response for input input_13', 'some response for input input_14', 'some response for input input_15', 'some response for input input_16', 'some response for input input_17', 'some response for input input_18', 'some response for input input_19', 'some response for input input_20', 'some response for input input_21', 'some response for input input_22', 'some response for input input_23', 'some response for input input_24', 'some response for input input_25', 'some response for input input_26', 'some response for input input_27', 'some response for input input_28', 'some response for input input_29', 'some response for input input_30', 'some response for input input_31', 'some response for input input_32', 'some response for input input_33', 'some response for input input_34', 'some response for input input_35', 'some response for input input_36', 'some response for input input_37', 'some response for input input_38', 'some response for input input_39', 'some response for input input_40', 'some response for input input_41', 'some response for input input_42', 'some response for input input_43', 'some response for input input_44', 'some response for input input_45', 'some response for input input_46', 'some response for input input_47', 'some response for input input_48', 'some response for input input_49', 'some response for input input_50', 'some response for input input_51', 'some response for input input_52', 'some response for input input_53', 'some response for input input_54', 'some response for input input_55', 'some response for input input_56', 'some response for input input_57', 'some response for input input_58', 'some response for input input_59', 'some response for input input_60', 'some response for input input_61', 'some response for input input_62', 'some response for input input_63', 'some response for input input_64', 'some response for input input_65', 'some response for input input_66', 'some response for input input_67', 'some response for input input_68', 'some response for input input_69', 'some response for input input_70', 'some response for input input_71', 'some response for input input_72', 'some response for input input_73', 'some response for input input_74', 'some response for input input_75', 'some response for input input_76', 'some response for input input_77', 'some response for input input_78', 'some response for input input_79', 'some response for input input_80', 'some response for input input_81', 'some response for input input_82', 'some response for input input_83', 'some response for input input_84', 'some response for input input_85', 'some response for input input_86', 'some response for input input_87', 'some response for input input_88', 'some response for input input_89', 'some response for input input_90', 'some response for input input_91', 'some response for input input_92', 'some response for input input_93', 'some response for input input_94', 'some response for input input_95', 'some response for input input_96', 'some response for input input_97', 'some response for input input_98', 'some response for input input_99']